I am doing page with gallery and if I click on one of them, it will redirect me to single gallery page and supposed to show photos of that gallery.
When I get on the page of actual gallery, there is nothing in gallery object.
When I do Gallery::with('photos')->get()
. This will give me every gallery with its photos. I want photos only from actual gallery.
Thanks.
//Photo model
class Photo extends Model
{
public function gallery()
{
return $this->belongsTo('App\Gallery');
}
}
//Gallery model
class Gallery extends Model
{
public function getRouteKeyName()
{
return 'slug';
}
/**
* Get the photos of this gallery.
*/
public function photos()
{
return $this->hasMany('App\Photo');
}
}
//show metod in GalleryController
public function show(Gallery $gallery)
{
$gallery->load('photos');
return compact('gallery');
}