laravel-collection

Laravel using Sum and Groupby

◇◆丶佛笑我妖孽 提交于 2019-12-02 02:22:31
I would like to fetch sum of quantity in each month, so that I can display on bar chart quantities against month This is what I thought but didn't workout $data1 = Borrow::groupBy(function($d) { return Carbon::parse($d->created_at)->format('m')->sum('quantity'); })->get(); My table structure Schema::create('borrows', function (Blueprint $table) { $table->increments('id'); $table->integer('member_id'); $table->integer('book_id'); $table->integer('quantity'); $table->integer('status')->default(0); $table->timestamps(); }); that a collection group by not an eloquent groupby if you want to do it

How to access the nth item in a Laravel collection?

烂漫一生 提交于 2019-11-30 23:15:10
问题 I guess I am breaking all the rules by deliberately making a duplicate question... The other question has an accepted answer. It obviously solved the askers problem, but it did not answer the title question. Let's start from the beginning - the first() method is implemented approximately like this: foreach ($collection as $item) return $item; It is obviously more robust than taking $collection[0] or using other suggested methods. There might be no item with index 0 or index 15 even if there

How to access the nth object in a Laravel collection object?

只愿长相守 提交于 2019-11-30 17:11:28
I have a laravel collection object. I want to use the nth model within it. How do I access it? Edit: I cannot find a suitable method in the laravel documentation . I could iterate the collection in a foreach loop and break when the nth item is found: foreach($collection as $key => $object) { if($key == $nth) {break;} } // $object is now the nth one But this seems messy. A cleaner way would be to perform the above loop once and create a simple array containing all the objects in the collection. But this seems like unnecessary duplication. In the laravel collection class documentation , there is

How to access the nth object in a Laravel collection object?

给你一囗甜甜゛ 提交于 2019-11-30 00:30:52
问题 This question was migrated from Software Engineering Stack Exchange because it can be answered on Stack Overflow. Migrated 5 years ago . I have a laravel collection object. I want to use the nth model within it. How do I access it? Edit: I cannot find a suitable method in the laravel documentation. I could iterate the collection in a foreach loop and break when the nth item is found: foreach($collection as $key => $object) { if($key == $nth) {break;} } // $object is now the nth one But this

How to Merge Two Eloquent Collections?

非 Y 不嫁゛ 提交于 2019-11-28 21:01:05
I have a questions table and a tags table. I want to fetch all questions from tags of a given question. So, for example, I may have the tags "Travel," "Trains" and "Culture" attached to a given question. I want to be able to fetch all questions for those three tags. The tricky, so it seems, is that questions and tags relationship is a many-to-many defined in Eloquent as belongsToMany. I thought about trying to merge the questions Collections as below: foreach ($question->tags as $tag) { if (!isset($related)) { $related = $tag->questions; } else { $related->merge($tag->questions); } } It doesn

Laravel collection converts array to object

人盡茶涼 提交于 2019-11-28 11:31:42
If I run $collection->filter(myFilter) , Laravel does this annoying thing of adding keys to each model in the collection like so: { "4": { "myObject": "data" }, "7": { "myObject": "data" } } How can I get rid of the "4" and "7" so it's an array of my objects? My code that runs is: $obj = Cars::with('brand')->orderBy('id')->get(); return $obj->filter(function($value, $key) { return $value->display == true; }); The issue is that the filter() method does not rekey the underlying collection array. So, the Collection is still representing an array, it is just that your array looks like this: [ 4 =>

Laravel pluck fields from relations

主宰稳场 提交于 2019-11-27 13:56:25
I have a Seller object which has a related User. I need to fill a select from LaravelCollective so I need to make something like this: {!! Form::selectGroup('seller_id', 'Seller', Seller::with('user')->pluck('user.first_name', 'id')->toArray(), null) !!} The problem is that I cannot take fields from relationships (user.first_name). How can I do it? UPDATE I want to avoid doing this... <?php $sellers = []; Seller::with('user')->get()->each(function ($seller) use (&$sellers) { $sellers[$seller->id] = $seller->user->first_name; }); ?> You can use Laravel's pluck method as: $sellers = Seller::with

How to Merge Two Eloquent Collections?

谁都会走 提交于 2019-11-27 11:51:54
问题 I have a questions table and a tags table. I want to fetch all questions from tags of a given question. So, for example, I may have the tags "Travel," "Trains" and "Culture" attached to a given question. I want to be able to fetch all questions for those three tags. The tricky, so it seems, is that questions and tags relationship is a many-to-many defined in Eloquent as belongsToMany. I thought about trying to merge the questions Collections as below: foreach ($question->tags as $tag) { if (

Laravel collection converts array to object

柔情痞子 提交于 2019-11-27 06:17:55
问题 If I run $collection->filter(myFilter) , Laravel does this annoying thing of adding keys to each model in the collection like so: { "4": { "myObject": "data" }, "7": { "myObject": "data" } } How can I get rid of the "4" and "7" so it's an array of my objects? My code that runs is: $obj = Cars::with('brand')->orderBy('id')->get(); return $obj->filter(function($value, $key) { return $value->display == true; }); 回答1: The issue is that the filter() method does not rekey the underlying collection

Eloquent Collection: Counting and Detect Empty

本小妞迷上赌 提交于 2019-11-26 15:36:42
This may be a trivial question but I am wondering if Laravel recommends a certain way to check whether an Eloquent collection returned from $result = Model::where(...)->get() is empty, as well as counting the number of elements. We are currently using !$result to detect empty result, is that sufficient? As for count($result) , does it actually cover all cases, including empty result? Gary Green When using ->get() you cannot simply use any of the below: if (empty($result)) { } if (!$result) { } if ($result) { } Because if you dd($result); you'll notice an instance of Illuminate\Support