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

By Hardik Savani November 5, 2023 Category : Laravel

Hello Friends,

In this tute, we will discuss laravel latest column. This tutorial will give you a simple example of how to pass optional argument latest() in laravel. you'll learn laravel order by column value. We will use laravel order by latest date. Follow the below tutorial step of laravel order by date format.

We are normally used latest() eloquent function to getting latest data with created_at default column. but if you need with another column like published_at or something then how you can do this.

I will show you simple example to pass an optional column with latest() query builder method. so, let's see the 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...

Tags :
Shares