Home > Mobile >  How to call function in order by clause in Laravel
How to call function in order by clause in Laravel

Time:01-01

How to call function written in modal in order by clause in Laravel

Controller

    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);
}

The function is written in modal

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');
}

I want to call allOrders() function to sort maximum orders by customer so customer will be on top who ordered most

Customer::withBlocked()->withInActivated()->orderBy(Customer::withAllOrders()->count(),"desc")->get(

CodePudding user response:

You can use

Customer::withCount('allOrders')->orderBy('all_orders_count', 'DESC');

and the continue your chain.

  • Related