Nevermind everyone, i solved my issue. the function is called as ->images; and not ->images();
Relationship belongsTo / hasMany returns nothing
Hey everyone,
I'm having trouble understanding why one of my 1:n relationships i defined with eloquent is not working right now. Basically I have a table for each slider and a table for the corresponding images.
I have set the id on the slider table as foreign key when migrating in the images table and seeded one slider with three images that correctly reference slider_id = 1 on phpmyadmin.
Slider Model:
class Slider extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'sliders';
/**
* Get the images for that slider
*/
public function images()
{
return $this->hasMany('App\Models\Slider\Slider_Image', 'slider_id');
}
}
Slider Image Model:
class Slider_Image extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'slider_images';
/**
* Get the slider that owns the image
*/
public function slider()
{
return $this->belongsTo('App\Models\Slider\Slider');
}
}
But when i try to call the relationship in my controller like below i do not receive any output at all.
$slider = Slider::find(1);
$images = $slider->images();
print_r($images);
If i try to query the images like using the image model everything works fine!
Slider_Image::where('slider_id', 1)->get();
Does anyone have a clue as to why the relationship does not return anything?
Please or to participate in this conversation.