Laravel关系一对多

问题描述

我是Laravel的新手,也许有人可以给我举个例子。
我有两个表-AuhorsBooks,还有两个视图-authors.indexbooks.index
我知道如何在authors.index中显示与作者相关的所有书籍,但问题是,如何在books.index视图中显示与作者相关的所有书籍?
我的模型和BookController:

Authors

Books

BooksController

解决方法

您需要在belongsTo模型中使用相反的函数Books

public function authors() {
    return $this->belongsTo(Authors::class,'author_id','id');
}

您可以找到更多信息here