A fluent, convenient wrapper for working with arrays of data provided by Illuminate\Support\Collection class.
Questions tagged [laravel-collection]
368 questions
2
votes
4 answers
Laravel 5 collection issue: Where not equal to
I am currently working on a modal where a user can insert an excel file. The task of the system is to upload and/or add a new database record if the records are new or identical to what exists in the database. BUT it also needs a delete function for…

Justin Boxem
- 153
- 1
- 3
- 18
2
votes
1 answer
Querying collections in Laravel
Have I got it correctly that when I query a Laravel collection, it doesn't query the database but executes the query on what was already fetched?
For example, I have a relation that returns a collection:
public function permissions()
{
return…

super.t
- 2,526
- 7
- 32
- 51
2
votes
1 answer
Amending a cast attribute inside a collection in Laravel
I have a model has an attribute which is cast to an array, like so
protected $casts = [
'data' => 'array',
];
I need to make an amendment to the array before returning the Collection. Using the each method on the Collection I can make changes…

DrKHunter
- 424
- 5
- 15
2
votes
1 answer
Filter a laravel collection inside a view by comparing dates?
I am trying to filter a collection of objects by comparing date its expiry date with today's date but I cant quite get it to work. Can anyone take a look at the code. I'm sure i'm missing something here. I have searched all over and found alot of…

matpulis
- 123
- 3
- 11
2
votes
1 answer
How can I use the reduce/reject/map function for Laravel collections?
I'm currently using the show function in my UserController.php to show all the users belonging to a particular company.
/**
* Show the list of all users.
*
* @return Response
*/
public function show() {
$users =…

maxxon15
- 1,559
- 4
- 22
- 35
2
votes
1 answer
Laravel Collection Get method not working as expected
I've tried the following in php artisan tinker:
>>> $contract = \App\ContractHour::where('user_id', '=', 14)->get();
=> Illuminate\Database\Eloquent\Collection {#747
all: [...]
}
>>> $contract->where('day_of_wk', 1);
=>…

adam78
- 9,668
- 24
- 96
- 207
1
vote
1 answer
How to modify keys of nested array in Laravel?
I have an array that has some nested arrays, I would like to transform all the keys into snake case. I am trying this:
$data = collect($array)->keyBy(function ($value, $key) {
return Str::snake($key);
})->toArray();
return…

DeveloperX
- 517
- 5
- 23
1
vote
0 answers
Laravel 9 - map collect can't scope outside variable array even using use
why map it's not returning or pushing a values into array i tried to use use function to call my array Emails but its not working here is my logic code
$emails = []; //emails array
$students = User::where('role','student')->get(); //10…

Ray Paras
- 185
- 11
1
vote
0 answers
How to paginate Laravel Analytics Collection data
I am using Laravel Analytics and calling Analytics data, I am trying to paginate the data but cannot:
Controller:
public function analytics()
{
$analyticsData = Analytics::fetchVisitorsAndPageViews(Period::days(1))->paginate(10);
$no =…

Anonymous Girl
- 582
- 8
- 22
1
vote
1 answer
How to sort parent data based on child column detial in laravel 5.4?
I have 3 way relationship firstl i have get code like this in a controller
Trial::with('subjects')->where('source_id', $sourceId)->get()->toArray()
Now I want to sort subject.reactions on desc order of subject.reactions.accounts.nb_followers…

Bhimani Rutvik
- 398
- 2
- 13
1
vote
3 answers
Laravel collection merge from array based on item key value
I have an array of $dates like below
[
"2022-30",
"2022-31",
"2022-32",
"2022-33",
"2022-34",
"2022-35",
]
and I have a $collection with an output like below
[
{
"new": 60,
"settled": "1",
"date":…

SymmetricsWeb
- 586
- 6
- 20
1
vote
0 answers
If Laravel collection has more than 1000 items the hasMany relationship collections are dissapearing
I have a Laravel query:
$var = Model::with(['rel1','rel2.1','rel2.2','rel2.3','rel3','rel4'])
->whereBetween('datum', [$start, $end])
->get();
If $var has more than 1000 items the hasMany relationships return an empty collection.
If the same query…

Zsolti
- 11
- 1
1
vote
1 answer
Distinct and sort collection based on duplicate count
I have the following collection
$collection = collect([
['id' => 1, 'name' => 'Design'],
['id' => 2, 'name' => 'Art'],
['id' => 3, 'name' => 'Comms'],
['id' => 2, 'name' => 'Art'],
['id' => 3, 'name' => 'Comms'],
['id' => 3,…

Fahad Shaikh
- 307
- 4
- 16
1
vote
1 answer
Laravel collection union during each not working
I have a probleme with union() that not insert my new collection
During my each I have the $mean collection with witch I search the slices() I test and have some $_availabaleSlices to insert into my $slices (id: 4 then id:7)
But after the whole code…

Stanislas Poisson
- 21
- 4
1
vote
2 answers
How to get nested key values in Laravel
Can someone please tell me how to echo or use nested key values. I basically get() this collection and I'm using foreach to do some further calculations.
My relation structure is zone.shipping and here I want to obtain fuel and emergency from the…

Manny
- 15
- 6