laravel collection to array
You can use toArray() of eloquent as below.
The toArray
method converts the collection into a plain PHP array. If the collection's values are Eloquent models, the models will also be converted to arrays
$comments_collection = $post->comments()->get()->toArray()
From Laravel Docs:
toArray also converts all of the collection's nested objects that are an instance of Arrayable to an array. If you want to get the raw underlying array, use the all method instead.
How to update an array inside of a Laravel Collection?
U can´t update the array cause is passed by value, for modify the content u need pass by reference like this:
foreach($data as &$item){
$item['status'] = 'NEW';
}
don´t forget to unset the reference:
unset($item);
I think it's best to avoid this pitfall altogether and just write foreach loops that have to manipulate the original array the normal way:
foreach($data as $index => $entry) {
$data[$index] = ...
}
here you will get more info about reference variables.
Laravel collection containing model to array
only()
will return an array of specified columns only. And toArray()
will transform the collection to array:
$collection->map(function($i) {
return array_values($i->only('username', 'email', 'date'));
})->toArray();
Mix an array on Laravel Collection
If you're trying to interleave items based on their type you can try :
$collection = collect($array)->groupBy(function ($value) {
return gettype($value);
});
$interleaved = $collection->first()->zip($collection->last())->flatten()->filter();
This will:
- Group items by type
- Take the first group and zip it with the last group (this assumes you have exactly 2 groups)
- It will then flatten the result and filter out null values
Note: You might need to add a check to determine if the string group is first or last and adapt accordingly.
Adding attributes to laravel collection
You can't use ->put
in an ->each
loop for this case, because ->each
does not loop the items by reference. So all modifications to the item are lost after the loop.
You can map the array to return your actual $item
merged with the extra key amount
. The correct method to use is ->map
.
$collection = $collection->map(function($item) {
return array_merge($item, [
'amount' => $item->price * $item->qty;
]);
});
https://laravel.com/docs/9.x/collections#method-map
Related Topics
Stream_Socket_Client Unable to Connect (Connection Timed Out)
How to See the Actual Xml Generated by PHP Soap Client Class
Show Hide Div If, If Statement Is True
How to Force Page Not to Be Cached in PHP
Yii2 Require All Controller and Action to Login
Subtracting a Certain Number of Hours, Days, Months or Years from Date
How to Use Composer to Autoload Classes from Outside the Vendor
Imagecreatefromjpeg and Similar Functions Are Not Working in PHP
How to Get the Length of Longest String in an Array
Is It Really That Wrong Not Using Setters and Getters
PHP & SQL Injection - Utf8 Poc
PHP Difference Between Notice and Warning
Php: Fopen() Permission Denied
Safe Alternatives to PHP Globals (Good Coding Practices)
Sending Data Using Post in Python to PHP
How to Remove Null Values from an Array
Stdclass Object and Foreach Loops
How to Get the Server Path to the Web Directory in Symfony2 from Inside the Controller