laravel - 如何制作没有重定向到页面 404 laravel 的 slug
问题描述
我尝试了一个不在我的数据中的 slug,并在
videodetail.php
public function detail($slug)
{
$video = Video::where('slug', $slug)->first();
$blogKey = 'blog_' . $video->id;
if(!Session::has($blogKey)) {
$video->increment('view_count');
Session::put($blogKey, 1);
}
$randomvideo = Video::whereHas('categories', function ($q) use ($video) {
return $q->whereIn('name', $video->categories->pluck('name'));
})
->where('id', '!=', $video->id)
->approved()
->published()
->take(8)->get();
return view('video.detail', compact('video', 'randomvideo'));
}
我在类别中收到此错误“在 null 上调用成员函数视频()”
public function videoByCategory($slug)
{
$category = Category::where('slug', $slug)->first();
$videos = $category->videos()->approved()->published()->paginate(21);
return view('category.index', compact('category', 'videos'));
}
解决方案
将检查添加到您的类别。
public function videoByCategory($slug)
{
$category = Category::where('slug', $slug)->first();
if ($category) {
$videos = $category->videos()->approved()->published()->paginate(21);
return view('category.index', compact('category', 'videos'));
} else {
// whatever you want to do
}
}
推荐阅读
- javascript - 返回包含过滤嵌套数组的新对象
- python - python按位运算的奇怪结果
- tensorflow - 在 Tensorflow 中训练模型时如何使用半正弦函数作为损失函数?
- r - 对变量排序并用另一个变量 R 分隔
- node.js - Vue.js 应用程序 - 视频元素拒绝在移动设备上流式传输
- javascript - 我尝试通过数据库验证数据以使用 Bootsrap 模型和 ajax 与 PHP 登录,但它不工作
- python - PCA -- 使用 Numpy 计算缩减大小的矩阵
- flutter - 如何用 bloc 过滤列表?
- java - 如何逐行比较两个文本文件并提取与另一个文件不同的两行?
- python - Python-如何将if条件中的值存储到带有附加的列表中