Hint 1:
You can define in model which table column is array or json. Then you don't need to json_encode and json_decode arrays when saving and retrieving data from json format database field.
protected $casts = [ 'attributes' => 'array'];
In saving, updating or inserting you can inject directly.
$record->attributes = $request['attributes'];
Hint 2:
Select inner object from json field
select('attributes->public_details as attributes')
attributes is the table json column and public_details is the inner object.
Hint 3:
You can map single attribute in json field.
where('attributes->phone', '386.547.4968')
attribute is the array and phone is the array element.
Hint 4:
There is lots of helper functions in MySql json format. In laravel you can use them as raw queries.
whereRaw("JSON_CONTAINS(attributes, '{$attributes}')")
$attributes is the json variable. ($atttributes is json)
public function findJsonEquals($attributes){// $data=$this->model->where('attributes->phone', '386.547.4968')// ->first();// return $data; $attributes=json_encode($attributes); $data=$this->model->whereRaw("JSON_CONTAINS(attributes, '{$attributes}')") ->first(); return $data; }
0 comments:
Post a Comment