A fluent, convenient wrapper for working with arrays of data provided by Illuminate\Support\Collection class.
Questions tagged [laravel-collection]
368 questions
351
votes
13 answers
Eloquent Collection: Counting and Detect Empty
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…

bitinn
- 9,188
- 10
- 38
- 64
134
votes
9 answers
How to Merge Two Eloquent Collections?
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…

Martyn
- 6,031
- 12
- 55
- 121
65
votes
6 answers
Eloquent collections: each vs foreach
Might not be a question specific to Eloquent collections, but it just hit me while working with them. Let's just assume we have a $collection object which is an instance of Illuminate\Support\Collection.
Now if we want to iterate over it, what are…

Niklas Modess
- 2,521
- 1
- 20
- 34
44
votes
4 answers
Laravel pluck fields from relations
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(),…

Alan
- 2,559
- 4
- 32
- 53
28
votes
5 answers
How to access the nth item in a Laravel collection?
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…

Džuris
- 2,115
- 3
- 27
- 55
27
votes
4 answers
How to access the nth object in a Laravel collection object?
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…

theHands
- 373
- 1
- 3
- 8
23
votes
9 answers
Laravel 5.2 pluck() multiple attributes from Eloquent Model Collection
Laravel 5.2 has pretty nice Helpers, I would like to use them to do following:
I have Eloquent Model Collection:
$lesson->users(); // returns Eloquent collection of 3 users
pluck() function would be useful, but it can get just single parameter.…

Fusion
- 5,046
- 5
- 42
- 51
18
votes
1 answer
Laravel Collection Date comparison
Alright so I have been looking for hours for an answer but can't seem to find one.
I have an array of "orders" all for different dates. I get all of the orders for this week, this should not be an issue, then I want to a tab for each day of the…

Oskar Jedvert
- 183
- 1
- 1
- 6
17
votes
2 answers
Laravel collection sortBy not taking effect
I'm trying to combine and sort the results from several db queries.
$events = collect();
$downedEvents = EventDowned::where('mission', $missionId)
->orderBy('mission_time', 'asc')
->get();
$events->push($downedEvents);
$getInOutEvents =…

Titan
- 5,567
- 9
- 55
- 90
14
votes
6 answers
How to convert Laravel collection array to json
How can i convert this Laravel collection array to json format like below.
//All records from users table.
$users = DB::table('users')->get();
// Required json format.
return '{
"data": [
{
"DT_RowId": "row_1",
…

Khirad Zahra
- 843
- 2
- 17
- 42
14
votes
2 answers
How to check if something is countable?
I have a var: $a. I don't know what it is. I want to check if I can count it. Usually, with only array, I can do this:
if (is_array($a)) {
echo count($a);
}
But some other things are countable. Let's say a Illuminate\Support\Collection is…

rap-2-h
- 30,204
- 37
- 167
- 263
12
votes
1 answer
How to return array with custom key mapWithKeys?
This is code that iterate collection in Laravel:
$usersData = $users->mapWithKeys(function ($item) {
return [$item->id => array("name" => $item->name, "email" => $item->email, "id" => $item->id)];
});
I tried to get array $usersData with…

POV
- 11,293
- 34
- 107
- 201
10
votes
5 answers
Natural ORDER in Laravel Eloquent ORM
How can i get 'natural order' in 'Eloquent ORM'? In table I have column 'text' (string).
Normal order: Model::orderBy('text')
'value 1'
'value 12'
'value 23'
'value 3'
'value 8'
I need this:
'value 1'
'value 3'
'value 8'
'value 12'
'value 23' …

Lajdák Marek
- 2,969
- 8
- 29
- 58
8
votes
2 answers
Reference next item in a Laravel collection
I am looping through a Laravel collection sorted by created_at to make a timeline. The start date for each item is created_at, and the end date is the created_at of the following item. If it is the last item, the event just lasts for a set 30…

samiles
- 3,768
- 12
- 44
- 71
7
votes
5 answers
How to use transform in paginated collection in laravel
I want to use map or transform in paginated collection in laravel 5.5 but I am struggling it work
This is what I was trying to do but getCollection is not available in LengthAwarePaginator as what we used to do in previous laravel versions see: How…

Theodory Faustine
- 432
- 2
- 10
- 22