Reputation: 767
I have small laravel project working on collection. I have string data in json format store in customdata column. Below is value in table.
someid somedata customdata
1 aaa [{"id": 1,"name":"new name","price": 20},{"id": 2,"name":"new name","price": 10}]
I can get data from that column and able to do some query. Let say as below dummy.
$result = '[{"id": 1,"name":"new name","price": 20},{"id": 2,"name":"new name","price": 10}]';
$collection = collect(json_decode($result, true));
return $collection->where('id', 1)->first();
Result look like below that work as need.
{
id: 1,
name: "new name",
price: 20
}
But I do not know how to add new/update/delete from collection. For example if I add more data(id=3) then it would be.
$result = '[
{"id": 1,"name":"new name","price": 20},
{"id": 2,"name":"new name","price": 10},
{"id": 3,"name":"new name","price": 15},
]';
It simply for model but I do not know for raw collection. Any advise or guidance would be greatly appreciated, Thanks.
Upvotes: 0
Views: 728
Reputation: 14261
Well, the Collection class has a lot of useful methods to play with. You can check the full list in the documentation.
To add an element to your collection, you can use the push()
method. From the docs:
The push method appends an item to the end of the collection:
$collection = collect([1, 2, 3, 4]); $collection->push(5); $collection->all(); // [1, 2, 3, 4, 5]
In your case:
// your actual collection
$result = /** here you get your json */;
$collection = collect(json_decode($result, true));
// the new element
$new = ['id' => 23, 'name' => 'the latest one!', 'price' => 1200];
//adding it to the collection
$collection->push($new);
To do this you have several paths.. the one that comes to my head is to generate a new collection mapping the new values in the element that matchs your condition. From the docs:
The map method iterates through the collection and passes each value to the given callback. The callback is free to modify the item and return it, thus forming a new collection of modified items:
$collection = collect([1, 2, 3, 4, 5]); $multiplied = $collection->map(function ($item, $key) { return $item * 2; }); $multiplied->all(); // [2, 4, 6, 8, 10]
So in your case:
$result = /** here you get your json */;
$collection = collect(json_decode($result, true));
// parameters to search and update
$id = 1 // the element id to find
$data = ['the', 'data', 'to', 'update'];
// updating the collection
$collection = $collection->map(function ($element) use ($id, $data) {
if ($element['id'])
$element = $data; // here goes your update logic
return $element;
});
To delete element of collection you can use the pull()
method. From the docs:
he pull method removes and returns an item from the collection by its key:
$collection = collect(['product_id' => 'prod-100', 'name' => 'Desk']); $collection->pull('name'); // 'Desk' $collection->all(); // ['product_id' => 'prod-100']
So to remove the element of the array, we will locate the element first with the search()
that return the key of the element to be removed, and then we use the previous mentioned method: pull()
$result = /** here you get your json */;
$collection = collect(json_decode($result, true));
// element to be deleted
$id = 1;
$key = $collection->search(function($element) use ($id) {
return $element['id'] == $id;
});
$collection->pull($key);
Upvotes: 1