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

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

In this article, You will Learn How to Pass an Optional Column to latest() in Laravel Eloquent.


Smit Pipaliya - Author - TechvBlogs
Smit Pipaliya
 

10 months ago

TechvBlogs - Google News

In this article, we will discuss How to Pass an Optional Column to latest() Laravel using Eloquent. This article 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 the latest date. Follow the below article step of laravel order by date format.

We normally use the latest() eloquent function to get the latest data with the created_at default column. But how can you do this if you need another column like published_at or something?

I will show you a simple example of passing an optional column with the latest() query builder method. So, let's see a simple example:

Controller:

<?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'));
    }
}

Thank you for reading this article.

If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.

Comments (0)

Comment


Note: All Input Fields are required.