Answer the question
In order to leave comments, you need to log in
Why can't get data from Laravel links?
there is a post model and a users model
link
public function whoCreated()
{
return $this->HasOne(User::class,'id', 'whoCreated');
}
Get the post Post::with('whoCreated')->find($id);
if translated into an array, then the data can be obtained $post['who_created']['name'];
but if you access the $post->who_created object, then it's empty, maybe you need to get the relation data in a different way?
Answer the question
In order to leave comments, you need to log in
//в модели Post
public function user()
{
return $this->belongsTo('App\User','user_id', 'id');
}
// в контроллере
Post::with('user')->find($id);
// в шаблоне
{{$post->user->name}}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question