首页 > 解决方案 > 如何用它的照片显示一个画廊

问题描述

我正在做带有画廊的页面,如果我点击其中一个,它会将我重定向到单个画廊页面并应该显示该画廊的照片。

当我进入实际画廊的页面时,画廊对象中没有任何内容。

当我这样做的时候Gallery::with('photos')->get()。这会给我每个画廊的照片。我只想要来自实际画廊的照片。

谢谢。

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

    }

标签: phplaravel

解决方案


画廊中没有任何内容,因为您没有返回视图。

这一行:

 return compact('gallery');

需要一个视图,例如:

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

或者,如果您只是展示带有 ajax 拉取功能的模型,则可以使用以下内容:

    $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

然后在您看来,$gallery->photos将为您提供该画廊的照片集。


推荐阅读