relationship - laravel 5 display books with author name -


how display books author name?
here model:

book.php

public function authors() {        return $this->belongsto('app\author'); } 

author.php

public function books() {         return $this->hasmany('app\book'); } 

my controller:

bookcontroller.php

use app\author;   public function index() {     foreach (book::with('authors')->get() $book)     {         echo $book->authors->name;     } } 

error

trying property of non-object

please might problem?

your code display author name each book.

instead use code returns books authors.

public function index()     {          $books = book::with('author')->get();      return $books; } 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -