如何在 Laravel 中按 order by 子句呼叫以模態撰寫的函式
控制器
public function index()
{
return Datatables::of(Customer::withBlocked()->withInActivated()->orderBy(Customer::withAllOrders()->count(),"desc")->get())
->addColumn('name', function ($customer) {
return "<a href=" . route('admin.module.show', ['moduleSlug' => request()->route()->parameter('moduleSlug'), 'module' => $customer->id]) . "><span class='font-weight-bold'>$customer->name</span></a>
<small class='block'>$customer->phone</small>";
})
->addColumn('orders', function ($customer) {
$OrderCount = Order::where("customer_id",$customer->id)->get()->count();
if($OrderCount > 1)
{ $orderText = $OrderCount.' Orders';}
else
{ $orderText = $OrderCount.' Order';}
return "<a href=" . route('admin.module.show', ['moduleSlug' => request()->route()->parameter('moduleSlug'), 'module' => $customer->id]) . "><span class='font-weight-bold'>".$orderText."</span></a>";
})
->addColumn('block', function ($customer) {
return '<div >
<input type="checkbox" data-id="' . $customer->id . '" id="activeBlock' . $customer->id . '" ' . ($customer->blocked ? 'checked' : '') . '>
<label for="activeBlock' . $customer->id . '">
<span >On</span>
<span >Off</span>
</label>
</div>';
})
->editColumn('created_at_human', function ($customer) {
return $customer->created_at->diffForHumans();
})
->addColumn('action', function ($customer) {
return '<a href="' . route('admin.module.edit', ['moduleSlug' => request()->route()->parameter('moduleSlug'), 'module' => $customer->id]) . '"><span id="editcustomer" data-id="' . $customer->id . '">
<i ></i>
</span></a>';
})
->rawColumns(['name','orders','block', 'created_at_human', 'action'])
->make(true);
}
該函式是用模態撰寫的
public function logisticsOrders()
{
return $this->hasMany(Order::class)->whereLogistics(true)->orderBy('created_at', 'desc');
}
public function allOrders()
{
return $this->hasMany(Order::class)->orderBy('created_at', 'desc');
}
public function currentOrders()
{
return $this->hasMany(Order::class)->whereLogistics(false)->whereMerchantApp(false)->whereIn('status', ['pending', 'assigned_to_merchant', 'ready_for_pickup', 'picked_up'])->orderBy('created_at', 'desc');
}
我想呼叫 allOrders() 函式來按客戶對最大訂單進行排序,這樣客戶就會在訂購最多的人中名列前茅
Customer::withBlocked()->withInActivated()->orderBy(Customer::withAllOrders()->count(),"desc")->get(
uj5u.com熱心網友回復:
您可以使用
Customer::withCount('allOrders')->orderBy('all_orders_count', 'DESC');
并繼續您的鏈條。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/399637.html
