Laravel relationships (one to many)

47 views Asked by At

I am new to Laravel and maybe somebody can give me an example for this.
I have two tables - Auhors and Books and two views - authors.index and books.index.
I know how to display all the books related to author in authors.index, but the question is, how to display all authors related books in books.index view?
My models and BookController:

Authors

Books

BooksController

1

There are 1 answers

2
Alberto Valerio On

You need to use the opposite function belongsTo into your Books model.

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

You can find more info here