dongqiancui9194 2019-07-28 15:12
浏览 51
已采纳

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

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

    }
  • 写回答

1条回答 默认 最新

  • dsaxw4201 2019-07-28 15:18
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程