LARAVEL 7 ||试图获取非对象的属性“名称”

时间:2020-06-02 04:08:56

标签: php eloquent laravel-7

我试图从作者表中获取名称,但是总是会发生此错误。我该怎么办?

模型 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

1 个答案:

答案 0 :(得分:2)

在这样的视图中更改代码: ... $data->author->name ... 不要使用“ id”