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.

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

报告相同问题?

悬赏问题

  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答