Laravel 5.1 DB:select toArray()
Seems like you need to cast the stdClass Objects as Array so you can have the structure you are looking for $result = array_map(function ($value) { return (array)$value; }, $result);
Seems like you need to cast the stdClass Objects as Array so you can have the structure you are looking for $result = array_map(function ($value) { return (array)$value; }, $result);
Use the static query method: $query = User::query(); Additionally, you can use the when method to chain these conditionals directly onto the query builder itself: $results = SomeModel::query()->when(condition(), function ($query) { $query->where(…); })->get(); This is functionally equivalent to the imperative if clause.
Document::groupBy(‘users_editor_id’) ->selectRaw(‘sum(no_of_pages) as sum, users_editor_id’) ->pluck(‘sum’,’users_editor_id’); // originally lists(), which was deprecated in favour of pluck in 5.2 // and dropped completely in 5.3 // ->lists(‘sum’,’users_editor_id’); // returns array like this: array( users_editor_id => sum, … ) Or this way (which I wouldn’t use, since it won’t be actual ORM result): Document::groupBy(‘users_editor_id’) ->selectRaw(‘*, sum(no_of_pages) as … Read more
These works but probably you didn’t use the right namespace, just use the use statement at the top of your class name like this: <?php namespace SomeNamespace; use App\Quotation; // Says “Quotation.php” is in “App” folder (By default in L-5.0) class someClass { //… } Then you may use in your method something like this: … Read more
You can save/insert serialized data in to a TEXT type field but for this you have to something using php, for example: $arr = array(‘x’, ‘y’, ‘z’); To insert this into a database field you can use serialize function like this $serializedArr = serialize($arr); Now, you can insert the $serializedArr array in to databse and … Read more
join() worked fine thanks to @rypskar comment $items = UserItems ::where(‘user_id’,’=’,$this->id) ->where(‘quantity’,’>’,0) ->join(‘items’, ‘items.id’, ‘=’, ‘user_items.item_id’) ->orderBy(‘items.type’) ->select(‘user_items.*’) //see PS: ->get(); PS: To avoid the id attribute (or any shared name attribute between the two tables) to overlap and resulting in the wrong value, you should specify the select limit with select(‘user_items.*’).
In your migrations you can do something like: $table->json(‘field_name’); And in your model you add the field to the $casts property to instruct Eloquent to deserialize it from JSON into a PHP array: class SomeModel extends Model { protected $casts = [ ‘field_name’ => ‘array’ ]; } Source: https://laravel.com/docs/5.1/eloquent-mutators#attribute-casting Note: This is also relevant answer … Read more