Reputation: 77
I have a category table that contains :
Schema::create('categories',function($table){
$table->increments('id');
$table->string('name',25);
$table->timestamps();
});
and product table :
Schema::create('products',function($table){
$table->increments('id');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories');
$table->string('name',25);
$table->text('description');
$table->timestamps();
});
and product_image :
Schema::create('images',function($table){
$table->increments('id');
$table->integer('product_id')->unsigned();
$table->foreign('product_id')->references('id')->on('products');
$table->string('src',100);
$table->timestamps();
});
And my models :
Category:
class Category extends Eloquent{
public function Test() {
return $this->hasMany('Product');
}
}
Product:
class Product extends Eloquent
{
public function category(){
return $this->belongsTo('Category');
}
public function images() {
return $this->hasMany('Images');
}
}
Now I want to show each category in a div with first product photo.
Route::get('test',function(){
$category = Category::all();
foreach($category as $cat){
dd($cat->Test->first()->images->first()->src);
}
});
but i receive this error :
Trying to get property of non-object
Could any one help me why I get this error ?
Upvotes: 0
Views: 269
Reputation: 4236
you missed () while calling realtion.
dd($cat->test()->first()->image()->first()->src);
Upvotes: 1