Home > Blockchain >  how to display the latest records in the db. in Laravel
how to display the latest records in the db. in Laravel

Time:06-18

my code to display the latest records in datable in laravel .now it's showing the first in the first. need to display the last data in first

if($status == 'pending'){

        $datas = Order::where('status','=','pending')->get();

    }

CodePudding user response:

Use orderBy Desc using primary key id or created_at

 $datas = Order::where('status','pending')->orderBy('id', 'desc')->get();

or

$datas = Order::where('status','pending')->orderByDesc('id')->get();

CodePudding user response:

if you want to get the last record use latest()->first()

if($status == 'pending'){

    $datas = Order::where('status','=','pending')->latest()->first();

}

and if you want to order

if($status == 'pending'){

    $datas = Order::where('status','=','pending')->orderBy('id', 'DESC')->get();

}
  • Related