I have a Laravel model with many attributes. So, I need iterate over these attributes. How can I do this? Something like this:
@foreach($model->attributes as $attribute)
// use $attribute
@endforeach
is it possible?
If you have an object, use getAttributes()
:
@foreach($model->getAttributes() as $key => $value)
// use $attribute
@endforeach
If these attributes were returned from a query, you can access the Model
class directly:
E.g.
@foreach (User::all() as $user)
<p>This is user {{ $user->id }}</p>
@endforeach
or, you can access the model reference given to a template:
E.g.
@foreach ($user->permissions as $permission)
<p>This is an user permission {{ $permission->id }}</p>
@endforeach
See more in the Laravel Docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With