如何按 Laravel 中的子句顺序调用函数
How to call function in order by clause in Laravel
如何按Laravel
中的子句顺序调用模态函数
控制器
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 class="custom-control custom-switch custom-control-inline">
<input type="checkbox" class="custom-control-input block-toggle" data-id="' . $customer->id . '" id="activeBlock' . $customer->id . '" ' . ($customer->blocked ? 'checked' : '') . '>
<label class="custom-control-label" for="activeBlock' . $customer->id . '">
<span class="switch-text-left">On</span>
<span class="switch-text-right">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 class="action-edit" id="editcustomer" data-id="' . $customer->id . '">
<i class="feather icon-edit"></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(
您可以使用
Customer::withCount('allOrders')->orderBy('all_orders_count', 'DESC');
然后继续你的链条。
如何按Laravel
中的子句顺序调用模态函数控制器
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 class="custom-control custom-switch custom-control-inline">
<input type="checkbox" class="custom-control-input block-toggle" data-id="' . $customer->id . '" id="activeBlock' . $customer->id . '" ' . ($customer->blocked ? 'checked' : '') . '>
<label class="custom-control-label" for="activeBlock' . $customer->id . '">
<span class="switch-text-left">On</span>
<span class="switch-text-right">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 class="action-edit" id="editcustomer" data-id="' . $customer->id . '">
<i class="feather icon-edit"></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(
您可以使用
Customer::withCount('allOrders')->orderBy('all_orders_count', 'DESC');
然后继续你的链条。