In the following code, the "liked_by_me" column remains unchanged but the "newAttr" column is added.
$commentList->transform(function($item, $key) {
$item->liked_by_me == null ? $item->liked_by_me = false : $item->liked_by_me = true;
$item->liked_by_me == null ? $item->newAttr = false : $item->newAttr = true;
return $item;
});
The $commentList collection is the result of a read from the DB using a standard model for the comments table. I am running Laravel 8
There seems to be no way to change an existing attribute in the rows of the resulting collection, but adding new attributes works. I have also tried various iterations using ->map() and ->each and nothing seems to allow me to alter an attribute of a row in a collection retrieved from the DB.
CodePudding user response:
use the map function, it works for me
$commentList->map(function($item){
//DO YOUR STUFF HERE
return $item;
});
CodePudding user response:
Don't use a null comparision with "==". Use is_null() function instead.