[英]Why this query don't work in Laravel 5.1?
Why this query: 为什么这个查询:
$data = Category::find(3)->articles->where('publish', '1')->paginate(5);
doesn't work? 不起作用? The
select
query: $data = Category::where('publish', '1')->paginate(5);
select
查询: $data = Category::where('publish', '1')->paginate(5);
works. 作品。
Version laravel: 5.1. 版本laravel:5.1。 In table
categories
there are one-to-many relationships with table articles
. 在表
categories
,与表articles
存在一对多关系。
articles
returns collection and articles()
returns relation
. articles
返回集合和articles()
返回relation
。 You can use where
on relation
and not on collection
. 您可以使用
where
的relation
,而不是collection
。
change 更改
$data = Category::find(3)->articles->where('publish', '1')->paginate(5);
to 至
$data = Category::find(3)->articles()->where('publish', '1')->paginate(5);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.