【发布时间】:2015-01-02 14:26:37
【问题描述】:
很抱歉修改了这个问题(致所有在此回答的人)。这个问题实际上是错误的。
$query = User::find(1)->books(); //books() is "return $this->hasMany('Book');"
$query_for_counting_history_books = clone $query;
$query_for_counting_geography_books = clone $query;
$number_of_history_books = $query_for_counting_history_books->where('type', '=', 'history')->count();
$number_of_geography_books = $query_for_counting_geography_books->where('type', '=', 'geography')->count();
$books = $query->paginate(10);
我希望 $books 被查询为:
SELECT * FROM books WHERE user_id=1 limit 10;
但它的输出是:
SELECT * FROM books WHERE user_id=1 and type="history" and type="geography" 限制 10 个;
- 我在这里缺少关于 克隆 的内容吗?
- 应该为这个解决方案做些什么?
【问题讨论】:
-
它按预期工作。你能检查一下你的
Illuminate\Database\Eloquent\Builder是否实现了__clone()方法吗? -
@Demodave 不,绝对不是