Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

matpol's avatar

orderby query not data received

Hi

Trying to do and orderby query e.g.

$query->orderBy($value, 'DESC');

When I test it I get a no data recieved error. Doing a 'normal' query it is fine

Any ideas?

Thanks

Matthew

0 likes
4 replies
matpol's avatar

This is the controller:

    public function index()
    {
        $q = Request::get('q');

        $action = Request::get('action');

        //$venues = Venue::take(20);


        //for drop ordering- would be good if this could be dynamic
        $columns = array(
            'name'          =>'Name',
            'client_id'     =>'Client',
            'contact_no'    =>'Contact No.',
            'contact_name'  =>'Contact Name'
        );

        if(Request::exists('q') && Request::exists('action')){

            //echo $q; exit;
            switch($action){
                case 'search':
                    $venues = Venue::search($q)->with('client')->paginate(20);
                    $title = 'Venues matching "'.$q.'"';
                break;
                case 'order':
                    $venues = Venue::orderby($q)->with('client')->paginate(20);
                    $title = 'Venues matching "'.$q.'"';
                break;
                default:
                    $venues = Venue::with('client')->paginate(20);
                    $title = 'All Venues';
                break;
            }
        } else {
            $venues = Venue::with('client')->paginate(20);
            $title = 'All Venues';
        }
    
    
        return view('venues.index', compact('title', 'venues','columns'));
    }

I am using a scope query to do the ordering:

    public function scopeOrderby($query, $value)
    {
        return $query->orderBy($value, 'DESC');
    }

The search query which is fine is this:


public function scopeSearch($query, $value)
    {
        return $query->where('name', 'LIKE', "%$value%")->orWhere('contact_name','LIKE', "%$value%");
    }

matpol's avatar
matpol
OP
Best Answer
Level 1

found problem the scope query had a conflict - just renamed this and it is now sorted

bobbybouwmann's avatar

There is already a function in Laravel, this might be the issue... Why don't you use that instead?

$query->orderBy('created_at', 'desc');

Here is the second parameter optionàl and default to `'asc'

Please or to participate in this conversation.