Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在使用雄辩关系的Laravel中使用OrderBy?_Laravel_Eloquent - Fatal编程技术网

如何在使用雄辩关系的Laravel中使用OrderBy?

如何在使用雄辩关系的Laravel中使用OrderBy?,laravel,eloquent,Laravel,Eloquent,我有两种型号:分类型号和邮递型号。 他们两人有关系。 在laravel视图中,我通过类别模型传递数据以显示PostAd数据。 我想在查询中应用OrderBy条件,但它不起作用 代码 我想这样做 $data['category'] = Category::with(['postads'])->where('id',$id)->orderBy('adtitle','DESC')->get(); 但它不起作用,因为它是类别模型。 我如何解决这个问题。如果您希望在之前获得订单的邮资数

我有两种型号:分类型号和邮递型号。 他们两人有关系。 在laravel视图中,我通过类别模型传递数据以显示PostAd数据。 我想在查询中应用OrderBy条件,但它不起作用

代码

我想这样做

$data['category'] = Category::with(['postads'])->where('id',$id)->orderBy('adtitle','DESC')->get();
但它不起作用,因为它是类别模型。
我如何解决这个问题。

如果您希望在之前获得订单的邮资数据,那么您可以在之前完成急切的加载订单

这里使用的是
get()
方法,所以应该是
first()
因为
->where('id',$id)
总是得到一条记录

$data['category'] = Category::with(['postads' => function($query) {
        $query->orderBy('adtitle', 'DESC');
    }])->where('id',$id)->first();

如果您想在之前获得订单的邮递数据,那么您可以在之前完成一个即时加载订单

这里使用的是
get()
方法,所以应该是
first()
因为
->where('id',$id)
总是得到一条记录

$data['category'] = Category::with(['postads' => function($query) {
        $query->orderBy('adtitle', 'DESC');
    }])->where('id',$id)->first();

向我们展示这两个模型之间的关系函数。向我们展示这两个模型之间的关系函数。如果有帮助,请选择答案。如果有帮助,请选择答案。