How to Pass an Optional Column to latest() in Laravel 10?

26-Apr-2023

.

Admin

How to Pass an Optional Column to latest() in Laravel 10?

Hi dev,

This tutorial will go over Laravel's most recent column. This article will show you how to pass the optional argument latest() in Laravel. Laravel order by column value will be taught to you. We'll utilise laravel order by most recent date. Follow the laravel order by date format tutorial step by step below.

We usually utilise the eloquent method latest() to fetch the most recent data with the created_at default column. However, if you require additional column, such as published_at, how can you accomplish this?

I'll show you a simple example of how to use the latest() query builder function to pass an optional column. So, here's a simple example:

Controller Code:


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

class PostController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$posts = Post::latest('published_at')->get();

return view('posts', compact('posts'));

}

}

I hope it can help you...

#Laravel 10