fbpx

OrderBy All Laravel

I tried running the following code and unfortunately, it throws an undefined method error.

// Throws an Error
$tickets = Tickets::orderBy('price','desc')->all();

When, you use orderBy you cannot use the all method, instead, you have to use the get method. So, I figured out that if I run the code below, I am able to order the collection based on the set field AND thus get all of the desired results:

$tickets = Tickets::orderBy('price','desc')->get();

Leave a Reply

Your email address will not be published. Required fields are marked *