dipcb05
1 month ago

How to access eloquent

Posted 1 month ago by dipcb05

i want to access models, in blade file.

example:

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>
@endforeach

but it didn't work

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

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.