0

How can i make a query to return me the most recent added objects

Controller :

public function __construct()
{
    $this->middleware(['auth', 'verified']);
}

public function index(Request $request){

    $object = Obj::with('children.objectable', 'ancestorsAndSelf.objectable')->ForTheCurrentTeam()->where(
        'uuid', $request->get('uuid', Obj::ForTheCurrentTeam()->whereNull('parent_id')->first()->uuid)
    )
    
    
        ->firstOrFail();

    return view('home', [
        'object' => $object,
        'ancestors' => $object->ancestorsAndSelf()->breadthFirst()->get(),
        'recent' => $object->orderBy('created_at','desc')->get(),
        dd($object),
    ]);

I am trying to make something like this but for the most recent added objects. How would i do it?

Juntrubal
  • 11
  • 4

2 Answers2

0

Use latest():

$object = Obj::with('children.objectable', 'ancestorsAndSelf.objectable')->ForTheCurrentTeam()->where(
        'uuid', $request->get('uuid', Obj::ForTheCurrentTeam()->whereNull('parent_id')->first()->uuid)
    )
    ->latest()
    ->firstOrFail();
kemp
  • 663
  • 7
  • 23
0

you can use order by desc and take a specified number of records

'recent' => $object->orderBy('created_at','desc')->take(5)->get()

this will take the 5 latest

toihirhalim
  • 55
  • 1
  • 2
  • 7