A fluent, convenient wrapper for working with arrays of data provided by Illuminate\Support\Collection class.
Questions tagged [laravel-collection]
368 questions
2
votes
2 answers
"Skip" method in a Laravel Collection
In the Query Builder (\Illuminate\Database\Query\Builder), it is possible to use both the skip($n) and take($n) methods.
In a Collection (\Illuminate\Support\Collection), it's possible to use the take($n) function, but there is no skip($n)…

Phiter
- 14,570
- 14
- 50
- 84
2
votes
3 answers
laravel collection remove item
I have the following Collection
Collection {#430 ▼
#items: array:54 [▼
0 => {#435 ▼
+"name": "Persona 5"
+"cover": "cover_persona-5_playstation-3.png"
+"bio": "This is a syno"
+"all_nb_rank": null
…

M.Pau
- 147
- 1
- 3
- 12
2
votes
1 answer
foreach on a Laravel collection
I'm pretty confused, I need to make a loop from a Laravel collection to print it a table.
My collection looks like:
The loop that I'm trying to do is something like:
@foreach($entries as $key => $value)
{{ $value->nombre…

pmiranda
- 7,602
- 14
- 72
- 155
2
votes
1 answer
How to sort a Laravel collection with sortBy(), ascending, and have NULL values placed last instead of first
Laravel's Collection class (v5.5) has a sortBy() method that sorts everything similar to if you had used a SQL ORDER BY statement, except there is one striking difference: a relational database will put NULL values at the end when sorting ascending…

fronzee
- 1,668
- 2
- 21
- 32
2
votes
3 answers
Sum of a PHP dynamic column
I would like to have the sum of the 4th column of a table, and I dry miserably.
My controller:
public function ticket()
{
$cmdbars = DB::table('bars')
->orderBy('updated_at', 'asc')
->get();
return…

Boss COTIGA
- 893
- 7
- 15
2
votes
2 answers
keyBy() doesn't change keys. Laravel 5.5
I'm working with laravel and made little api. I have problems with keyBy().
I get response like this
"translate": [
0: {"id": 0, "lang": "az" },
1: {"id": 0, "lang": "ru" },
2: {"id": 0, "lang": "en" }
]
And I want to change keys of…

rufatZZ
- 743
- 2
- 11
- 27
2
votes
1 answer
How to get multiple whereBetween records in Laravel
I'm building a small application on Laravel 5.4 where I'm having data sets to are being fetched from the relationship something like this:
$meetings = Company::where('name', 'Atlanta Ltd')
->withCount(['interactions as investors'=> function ($q)…

Nitish Kumar
- 6,054
- 21
- 82
- 148
2
votes
1 answer
Laravel collection order similar to DB FIELD
I have a query with following order
$query->orderByRaw("FIELD(type, 'red', 'green', 'aqua') ASC");
This will order items not alphabetically, but specifically by the value.
Is there is a way to do same thing in Laravel collection?
Like:
$collection =…

SergkeiM
- 3,934
- 6
- 36
- 69
2
votes
2 answers
Get count of unique values with the column name in Laravel 5.2 Collection
I am trying to get the number of unique Brands from my Products Table with their count from a Laravel collection.
I was able to do that using a specific query for the products but the reason i'm using a collection now is because i also want to get…

user3659497
- 445
- 1
- 6
- 15
2
votes
2 answers
Transform collective to array in Laravel, Laravel 5
How can I transform this collection to array and eject "whereNotIn" using a Laravel query, like this:
->whereNotIn('id', ['collection'])->get();'
Collection {#259 ▼
#items: array:3 [▼
0 => {#257 ▼
+"id": 2
}
1 => {#256 ▼
+"id": 3
}
2 => {#237…

Renato
- 47
- 8
2
votes
1 answer
What does Laravel 'find' method return
A simple MySQL table namely centers is out there with Center as its model and code as one of the columns.
$center_found=Center::find(6);
echo " code from method 1 = ".$center_found->code;
echo " code from method 1 = ".$center_found['code'];
echo…

Istiaque Ahmed
- 6,072
- 24
- 75
- 141
2
votes
3 answers
how to add extra element in laravel collection
i would like to know how can i add extra column in laravel collection, return from database. for instance.
User Model
User->id
User->name
Reservation Model
Reservation->user_id
Reservation->id
now
$users = User::all();
$user_reservation =…

Muhabutti
- 1,256
- 2
- 15
- 20
2
votes
1 answer
Laravel collection - flatten is not working
I have student database in which I have only 3 columns (id, name, dob). I've written simple select query,
return DB::table('student')->get(['id','name','dob']);
I'm getting response,
[{"id":1,"name":"Kaylah…
user3757453
2
votes
2 answers
Transform specific fields in Laravel?
How do I Transform specific fields in Laravel?
For example:
$user = \Auth::User();
return [
'user_id' => $user->id,
'articles' => $user->articles,
'pages' => $user->pages,
];
$user->articles will show the entries the articles table…

I'll-Be-Back
- 10,530
- 37
- 110
- 213
2
votes
4 answers
How to check if collection contains a value in Laravel
I am unable to check whether following collection contains data or not
$users = \App\Tempuser::where('mobile','=',$request->mobile)->get();
if(isset($users))
return "ok";
else
return "failed";
but if there is nothing in $users still i am not…

SamD
- 185
- 5
- 22