OrderBy 未在 laravel 上运行

OrderBy its not wokring on laravel

我在做“orderBy”名称产品时遇到问题。这是我的代码

$resume =  Transaction::with(['product' => function ($q) {
    $q->orderBy('name_product','ASC');
}])
->where('status', 'keluar')
->where('status', 'masuk')
->get();

但我的代码不起作用...这是输出 result

因为您正在将条件应用于 product,而不是 transaction

我有另一个建议使用 whereHas

Transaction::whereHas('product', function($query) {
  $query->->orderBy('name_product','ASC');
})->where('status', 'keluar')
->where('status', 'masuk')
->get();

改为使用集合进行排序。它允许基于嵌套的 属性.

进行排序
$resume =  Transaction::with('product')
->where('status', 'keluar')
->where('status', 'masuk')
->get()
->sortBy('product.name_product')
->values();