PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 20, 2022

[FIXED] How to call function in order by clause in Laravel

 January 20, 2022     eloquent, laravel, php     No comments   

Issue

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

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(

Solution

You can use

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

and the continue your chain.



Answered By - Abdul Rehman
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing