A fluent, convenient wrapper for working with arrays of data provided by Illuminate\Support\Collection class.
Questions tagged [laravel-collection]
368 questions
4
votes
2 answers
Laravel collections: Flatten with full key name
Is there a way with Laravel Collections to flatten an array with key "namespace". Something like:
$a = collect([
'id' => 1,
'data' => [
'a' => 2,
'b' => 3
]
]);
$a = $a->flattenWithKeysNamespace(); // <-- this does not…

rap-2-h
- 30,204
- 37
- 167
- 263
4
votes
3 answers
How to sort a collection of UTF-8 strings containing non-Latin chars in Laravel 5.3?
Folks, I want to sort following nested collection by string alphabeticaly:
$collection = collect([
["name"=>"maroon"],
["name"=>"zoo"],
["name"=>"ábel"],
["name"=>"élof"]
])->sortBy("name");
I would expect:
1=> "ábel"
2=> "élof"
3=>…

Fusion
- 5,046
- 5
- 42
- 51
3
votes
2 answers
Laravel cursorPagination returns results with a mix of array and objects
I'm implementing infinite pagination into a Livewire component and I'm noticing that on initial page load it's loading objects, but when I scroll and load more data its adding arrays to the collection so I'm ending up with a collection with mixed…

Jonathon
- 312
- 2
- 10
3
votes
3 answers
Load specific columns from eager loaded laravel collection
Consider books and authors relationship. Every book belongsTo an author and 1 author hasMany books. Books table has fields like (Id,Title,Version etc) and authors have fields like (Id,Name,Address etc). Note DB columns do not follow the default…

Mussa Moses
- 101
- 9
3
votes
1 answer
What is use of attach() method in Laravel?
i dont have a idea what is use of this method i saw something on someone code like this.
$user = JWTAuth::parseToken()->authenticate();
$new_car = new Car();
$new_car->name = $request->name;
$new_car->age = $request->age;
$new_car->model =…

Ray Paras
- 185
- 11
3
votes
1 answer
How add/append new items by key Laravel Collection
I don't know if it's the best or right way to do it, but in PHP, I simply would.
$array = [];
$array['CODE-123'][] = [
'name' => 'NAME 001',
'lastname' => 'LASTNAME 001',
];
…

Magno Alberto
- 628
- 14
- 27
3
votes
4 answers
How to pluck multiple columns in Laravel
I want to get a student's full name, but in my database, I have two different columns: first_name and last_name. I want to get both of these columns at the same time.
Controller
public function getStudentName($id)
{
$students =…

Hasnain Kahn
- 119
- 1
- 3
- 13
3
votes
1 answer
Laravel Collection update elements in loop
I declared a variable of collect() data type. I want to iterate through it, and to update specific column on each row.
The example code:
$a = collect([
['one' => 1, 'two' => 2],
['one' => 3, 'two' => 4],
['one' => 5,…

TheVic
- 303
- 6
- 16
3
votes
2 answers
Laravel: collect() helper - set type of collection
If I do this:
$obj = factory(Object::class)->make();
collect($obj);
I am returned a collection of type:
Illuminate\Support\Collection
Laravel also lets you define your own collections with their specific methods. In the model, you do:
public…

iateadonut
- 1,951
- 21
- 32
3
votes
0 answers
Laravel Collections. forget() on nested key
Is it possible to use some neat Laravel collection method similar to forget(), that will work with nested arrays.
Here is an example of what I want.
I have such a Collection instance:
And I want to drop coordinates column (all items are identical)…

D.R.
- 2,540
- 3
- 24
- 49
3
votes
2 answers
Laravel Collection. Map one keys to other
I want to map some keys in Laravel collection to other, that are stored in an array.
I can't "invent" a proper neat and short pipeline for such a transformation.
Here is a simplified example of what I want:
$mappedKeys = [
'1' => 'One',
'2'…

D.R.
- 2,540
- 3
- 24
- 49
3
votes
2 answers
Laravel - replace null with empty array when no relation is found
Is it possible to replace null with an empty array when no relation is found?
E.g. The customer has contacts and contracts but one of the contract has no web.
$customers = Customer::with('contacts', 'contracts.web')
…

Ilario Engler
- 2,419
- 2
- 27
- 40
2
votes
2 answers
How to update Laravel Eloquent column cast as collection
I am using Laravel 10.
I am utilizing casting for a JSON column in the following manner:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Item extends Model
{
protected $casts = [
'meta' => 'collection', // here
…

Denis
- 322
- 1
- 4
- 15
2
votes
1 answer
How to apply custom keys to my sequential array/collection of arrays using Laravel or PHP?
I'm trying to apply custom keys to a collection in laravel.
The best way that I found was using functions like transform or mapWithKeys.
$result = $collection->map(function ($item) {
return [
'custom_key_name_1' => $item[0],
…

FabianoLothor
- 2,752
- 4
- 25
- 39
2
votes
1 answer
How can I remove filtered values from a Laravel collection within a for loop?
I would basically like to remove values from $collection to reduce the for loop duration since the filtered values are used only once.
$collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
foreach ($collection as $k => $v) {
$filtered =…

Howard
- 3,648
- 13
- 58
- 86