php - 如何用它的照片显示一个画廊
问题描述
我正在做带有画廊的页面,如果我点击其中一个,它会将我重定向到单个画廊页面并应该显示该画廊的照片。
当我进入实际画廊的页面时,画廊对象中没有任何内容。
当我这样做的时候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');
}
解决方案
画廊中没有任何内容,因为您没有返回视图。
这一行:
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
将为您提供该画廊的照片集。
推荐阅读
- python - 为什么我的 Python 代码是从不同的目录导入的?
- android - 无法取消隐藏/隐藏 ProgressBar Android
- javascript - 在函数内返回一个字符串
- python - 如何将现实世界 5 天的每日股票数据转换为每周模式以获得每周移动平均线
- python - Google colab:从 Github pandas 中读取 .xlsx 文件
- python - 我如何对类进行排序以匹配特定特征
- google-colaboratory - 增加 Colab Pro 运行时限制
- r - R未来:显示进度+等到完成
- python - 将数据添加到列并附加它
- c# - c#LINQ如何正确地将方法传递给select并返回值