问题描述
我是Laravel的新手,也许有人可以给我举个例子。
我有两个表-Auhors
和Books
,还有两个视图-authors.index
和books.index
。
我知道如何在authors.index中显示与作者相关的所有书籍,但问题是,如何在books.index
视图中显示与作者相关的所有书籍?
我的模型和BookController:
解决方法
您需要在belongsTo
模型中使用相反的函数Books
。
public function authors() {
return $this->belongsTo(Authors::class,'author_id','id');
}
您可以找到更多信息here