如何拆分一个有说服力的模型查询 [英] How to split up an Eloquent model query
本文介绍了如何拆分一个有说服力的模型查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试打破这样的雄辩查询
I'm trying to break up an Eloquent query like this
$query = new Product;
if (!empty($req->query['id'])) {
$query->where('id', '=', '1');
}
$products = $query->get();
上面的结果给了我数据库中的所有产品。然而,这实际上是有效的。
The result above gives me all products in the database. This however, does in fact work.
$products = Product::where('id', '=', '1')->get();
有没有办法这样做?
推荐答案
在Laravel 4中,您需要将查询参数附加到查询变量中。在Laravel 3中,它会像你在做的那样工作。
In Laravel 4 you need to append your query parameters to your query variable. In Laravel 3, it would work like you're doing.
这是你需要做的(我不确定它是否适用于新产品
tho):
This is what you need to do (I'm unsure if it will work with new Product
tho):
$query = new Product;
if (!empty($req->query['id'])) {
$query = $query->where('id', '=', '1');
}
$products = $query->get();
这篇关于如何拆分一个有说服力的模型查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文