Laravel雄辩的`take`和`orderBy` [英] Laravel Eloquent `take` and `orderBy`
本文介绍了Laravel雄辩的`take`和`orderBy`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试使用"take"和"orderBy"查询中的每一个时,模型都会返回一些记录:
When I try to use each of "take" and "orderBy" query, the Model return some records:
$this->hasMany("App\User")->take(3)
$this->hasMany("App\User")->orderBy("id", "desc")
但是当我组合它们时,它返回一个空数组:
But when I combine them, it return a null array:
$this->hasMany("App\User")->take(3)->orderBy("id", "desc")
我运行原始sql(来自toSql()函数) 并按我的预期返回了3条记录. 我遇到什么错误?
I run the original sql (from toSql() function) and it return 3 records as I expect. What mistake I get?
推荐答案
您需要更改顺序,它将是:
You need to change the order, it will be:
$this->hasMany("App\User")->orderBy("id", "desc")->take(3)
此->take(3)
将执行mysql查询,因此您首先需要将orderBy("id", "desc")
添加到hasMany关系中.
This ->take(3)
will execute the mysql query, so you first need to add the orderBy("id", "desc")
to the hasMany relation.
这篇关于Laravel雄辩的`take`和`orderBy`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文