如何用它的照片显示一个画廊

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');

    }

There is nothing in the gallery because you are not returning a view.

This line:

 return compact('gallery');

Needs a view, something like:

return view("somethingFolder.somethingView", compact('gallery'));

OR if you are just showing a model like with an ajax pull, you can use something like this:

    $view = view('somefolder.someview', compact('gallery'));

    $sections = $view->renderSections(); // returns an associative array of 'content', 'pageHeading' etc

    return $sections['modalContent']; // this will only return whats in the content section

Then in your view, $gallery->photos will give you a collection of the photos for that gallery.