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'));
}
}
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial