我有控制器,其中有以下代码:
public function index()
{
$posts = Post::orderByDesc('id')->paginate(15);
return view('home', compact('posts'));
}这将返回一个页面中的所有帖子。页面上有选项卡:all posts、posts by time、posts by rating。
在模型中,我有这样的范围:
public function scopeOfType($query, $type)
{
return $query->where('type', $status);
}我怎么才能在我有标签的页面上调用这个呢?
当我尝试调用作用域时:
@forelse($posts->ofType($type) as $post)我得到了错误:
Method Illuminate\Database\Eloquent\Collection::ofType does not exist. (View: 我怎么才能解决这个问题?
发布于 2018-05-17 12:44:11
如果要按类型循环,则可以使用集合上的where:
@forelse($posts->where('type', $type) as $post)注意,这是一个集合,它根据给定的类型对其进行筛选。
https://stackoverflow.com/questions/50391270
复制相似问题