$book <-> $books?
Oct 15, 2014
38
Level 51
Undefined Property/Non-object on Relationship of 2 tables
Hi All,
I have a authors & books table With two models like so:
class Authors extends \Eloquent {
protected $guarded = ['id'];
public $table = 'authors';
public $timestamps = false;
public function book()
{
return $this->hasMany('Books', "author", "last_name");
}
}
class Books extends \Eloquent {
protected $guarded = ['id'];
public $timestamps = false;
public function author()
{
return $this->hasOne('Authors');
}
}
My controller then looks like:
public function authorBooks($name)
{
$books = Authors::where('last_name', '=', $name)->get();
$num = 1;
$pages = Pages::where('status', '=' , $num)->get();
$categories = Categories::where('parent_id','=', 0)->get();
return View::make('site.authorbooks', compact('pages', 'categories', 'books'));
}
Route
Route::get('/authors/{name}','FrontController@authorBooks');
Then inside my view i call
{{ $book->book->first()->title }}
Shows
Trying to get property of non-object (View: /home/vagrant/code/bookstore/app/views/site/authorbooks.blade.php)
Can anyone help as to where I'm going wrong here?
Please or to participate in this conversation.