Eloquent 急切加载 Order by [英] Eloquent eager load Order by

查看:32
本文介绍了Eloquent 急切加载 Order by的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 eloquent 查询有疑问.我正在使用急切加载(一对一关系)通过考试"获得学生",使用下面的代码.

I have problem with eloquent query. I am using eager loading (one to one Relationship) to get 'student' With the 'exam', Using the code below.

Student::with('exam')->orderBy('exam.result', 'DESC')->get()

我想通过考试"中的结果"列对收到的行进行排序.我正在使用

And i want to order received rows by the 'result' column in 'exam'. I am using

->orderBy('exam.result', 'DESC')

但是它不起作用.任何想法如何做到这一点?

But it is not working. Any ideas how to do it ?

推荐答案

试试这个:

Student::with(array('exam' => function($query) {
        $query->orderBy('result', 'DESC');
    }))
    ->get();

这篇关于Eloquent 急切加载 Order by的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆