Answer the question
In order to leave comments, you need to log in
How to add element to Laravel collection?
There is a standard label with users users.
There is a selection:
$users = Users::all();
In the course of the code in the
foreach ($users as $user){
...
here you need to add a new field to $users
}
This is necessary so that later it is convenient to pull out new, additional data in the form.
Clearly here:
Answer the question
In order to leave comments, you need to log in
$user->new_property = "some value";
$user-save();
Accordingly, the new property must be written in the fillable in the model and entered into the table. If I understand the question correctly
You need to add a get-set for the attribute and write it in $appends so that there are no problems with serialization https://laravel.com/docs/5.6/eloquent-serializatio...
foreach ($users as $user){
$user->setAttribute($key,$val)
}
This is if you don't need to touch tables. The lifetime is the current request. It can be convenient to stuff a collection with all the calculated elements and pass it as an interface to the template engine.
here are the methods of the Eloquent model.
https://laravel.com/api/5.7/Illuminate/Database/El...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question