我试图从作者表中获取名称,但是总是会发生此错误。我该怎么办?
模型 App \ Book
public function author()
{
return $this->hasOne('App\Author');
}
App \ Author
public function book()
{
return $this->belongsTo('App\Book');
}
观看次数 作者/节目...
@php
$book = Book::with('author')->where('author_id',$author->id)->get();
@endphp`
@foreach ($book as $data)
...
Author name : {{$data->author_id->name}}
...
@endforeach
答案 0 :(得分:2)
在这样的视图中更改代码:
...
$data->author->name
...
不要使用“ id”