1 month ago

How to access eloquent

Posted 1 month ago by dipcb05

i want to access models, in blade file.


in user model

public function posts()
    return $this->hasMany(posts::class, 'users_id', 'id');

in posts class

  public function user()
        return $this->belongsTo(User::class, 'users_id', 'id');

now i want to access user class through post class in blade file

@foreach($posts as $post)
<h1> {{ $post->user()->name }} </h1>

but it didn't work

                                                   {{ $use = App\User::find($post->users_id); echo $use->name;}}

i write a raw php code for it , it works. i want to do it with eloquent for saving time. is it there any option?

Please sign in or create an account to participate in this conversation.