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
Related
I have created a relationship between products and users using a foreign key called "previous_owner". I can see when I dump the value that I'm passing to the view that the data I'm after is available under:
$product->relations->product_owner->user->attributes->name
But how do I access it from within my view. I'm trying to loop through the products and then do something like:
{{ $product->product_owner->user->name }}
or
{{ $product->product_owner->name }}
But non of it works, keep getting:
" Trying to get property 'name' of non-object "
In your view code must be similar this
#foreach($products as $product)
{{ $product->product_owner->name }}
#endforeach
This code mean each product has product_owner. When one product does not have product_owner that case value of $product->product_owner is Null and then your code is null->name and you get error. For fix it you must be check relation is null or use ?? operator. Change your code to
#foreach($products as $product)
{{ $product->product_owner->name ?? ''}}
#endforeach
or
#foreach($products as $product)
#if($product->product_owner)
{{ $product->product_owner->name}}
#endif
#endforeach
If name is attribute and product_owner is relationship function then try the below:
{{ $product->product_owner()->name }}
Your product_owner isn't stdClass. It is array. So you can't access this key of this array with "->" operator. try
{{ $product->product_owner['name'] }}
I want to explode array value and have successfully doing so by using this code :
#foreach(explode('.', $comment->topic_id) as $topic)
{{ $topic }}
#endforeach
This is the output
Topic : 1,2
The problem is, I want to implement relation belongsTo to the topic_id. When I add the relation and run the code, unfortunately only one of the value is shown.
#foreach(explode('.', $comment->getTopic->topic) as $topic)
{{ $topic }}
#endforeach
This is my model
public function getTopic()
{
return $this->belongsTo('App\Topic', 'topic_id', 'id');
}
Output :
Topic : Laravel
What is the right way to call this array? Please help me. Thank you.
You can't use relationship in this case. If you simply looking for solution then you could do something like the following:
<?php $topic_ids = explode('.', $comment->topic_id);
$topics = App\Topic::whereIn(id, $topic_ids)->get();
?>
#foreach($topics as $topic)
{{ $topic }}
#endforeach
BTW you should structure your database more efficiently.
#foreach(explode(',', $item->chapter_id) as $layer)
<?php
$chapters = DB::table('topic_types')->where('id', $layer)->get();
?>
#foreach ($chapters as $ch)
{{$ch->name}} <br>
#endforeach
#endforeach
I know that I can get object after save method call. But how can I get relational data using eager loading. So that I dont have to give a call to DB for each relation.
Use the with() method to eager load relations:
$collection = Model::with('relation')->get();
Then you'll be able to iterate over collections to get objects:
#foreach ($collection as $object)
{{ $object->name }}
#foreach ($object->relation as $relatedObject)
{{ $relatedObject->name }}
#endforeach
#endforeach
I use Laravel with foreach in a blade.php
{{ $users = App\User::where('user_id', $post->user_id)->get() }}
and I got the result of this which show on the page
[{"user_id":"123","email":"123#com","password":"1234","first_name":"Tony"}]
But I want to get "Tony" only, how can I call?
{{ ($users = App\User::where('user_id', $post->user_id)->first())->first_name }}
Or better in sense of performance, if you select posts with user:
{{ $post->user->first_name }}
But it is better to prepare necessary data in controller.
get() returns an array, you should use first()
{{ ($users = App\User::where('user_id', $post->user_id)->first())->first_name }}
Since you said you are using foreach in blade. Try something like this
#foreach($user as $u)
{{$u->first_name}}
#endforeach
This will give you first_name for all users
Is there any way to sort #foreach loop in laravel blade?
#foreach ($specialist as $key)
<option value="{{$key->specialist_id}}"> {{$key->description}}</option>
#endforeach
I wolud like to order by $key->description,
I know I can use order by in my controller,
->orderBy('description')
but the controller returns other values and those values must be sorted as well, so I need to order by in blade.
Assuming that your $specialist variable is an Eloquent collection, you can do:
#foreach ($specialist->sortBy('description') as $oneSpecialist)
<option value="{{ $oneSpecialist->specialist_id }}"> {{ $oneSpecialist->description }}</option>
#endforeach
Moreover, you could call your Eloquent model directly from the template:
#foreach (Specialist::all()->sortBy('description') as $oneSpecialist)
<option value="{{ $oneSpecialist->specialist_id }}"> {{ $oneSpecialist->description }}</option>
#endforeach
Note that you are using a misleading variable name of $key in your foreach() loop. Your $key is actually an array item, not a key. I assume that you saw foreach ($array as $key => $value) syntax somewhere and then removed the $value?
I would suggest using a laravel collection. More specifically, the sortBy(). You can use either of these in your view or the controller that you are passing the data from. If the data is being passed by a model, be sure to use the collect() function before proceeding to use any of the others listed.
Hope this helps!
Even if the order fails you can use ->values() to re-index the elements
$collection->sortBy('key')->values()