提问人:Marcel Salazar 提问时间:7/25/2023 最后编辑:Marcel Salazar 更新时间:7/25/2023 访问量:52
根据嵌套模型的字段对雄辩查询进行排序
Sort an eloquent query based on the field of a nested model
问:
我有一个雄辩的查询,它以分页的方式为我带来了 10 份报告,其中包含它们各自的交易和合同
$reports = Report::with([
"transaccions",
"status_report",
"contracts",
])
->where(function ($query) use ($filter, $dates, $type_report, $dataservice) {
$query
->where("type_report", ($dataservice == 'true' ? '<>' : '='), $type_report)
->whereBetween("created_at", [$dates[0], $fechas[count($dates) - 1]])
->whereHas('status_report', function ($q) {
$q->whereIn("status", ["VERIFYED"]);
})
->where(function ($query) use ($filter) {
$query->where("id", "like", '%' . $filter . '%')
->orWhereHas('contracts', function ($query) use ($filter) {
$query->where('contract', 'like', '%' . $filter . '%')
->orWhereHas("rif", function ($query) use ($filter) {
$query->where("rif", 'like', '%' . $filter . '%');
})
->orWhere("name", 'like', '%' . $filter . '%');
})->orWhereHas('transaccions', function ($query) use ($filter) {
$query->where('ref', 'like', '%' . $filter . '%')
->orWhere('amount', 'like', '%' . $filter . '%');
});
});
})
->paginate($length);
此查询显示在另一个函数中处理的结果,并显示如下内容
{
"id": 45751,
"status": "VERIFYED",
"type_report": "MENSUALITY",
"nota": "",
"transaccions": [
{
"id": 46358,
"ref": "22380011359",
"voucher": false
}
],
"total": "1,28",
"contracts": [
{
"name": "PERSON",
"contract": "123456",
"rif": "1122334455",
"status": "ACTIVE"
}
],
"created_at": "2022-08-26 14:18:00"
},
这些结果显示在表格中,我希望能够根据 transactions.amount、contracts.contract(或嵌套模型中的任何其他字段)对结果进行排序 但是我找不到方法,我知道你可以这样对嵌套模型进行排序:
"transaccions" => function ($query) use ($order_by, $order) {
$query->orderBy($order_by, $order);
},
但我需要对所有结果进行排序,因为这只对嵌套对象进行排序,谢谢。
答: 暂无答案
评论
join()
with()
JOIN ...
transactions.amount
>groupBy($order_by, "report.id")->orderBy($order_by, $order)
report.id
contracts.contract
->join()
->with()
select
join
report
transactions
contracts
contracts
report
10
12
report
11
report
report
report
contracts
contract