How to Pass an Optional Column to latest() in Laravel Eloquent?
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Get Array of Ids from Eloquent Models in Laravel
- Laravel Where Clause with Function Query Example
- Laravel Eloquent orWhereHas() Condition Example
- Laravel Case Sensitive Query Search Example
- Laravel Eloquent Left Join Where Null Condition Example
- Laravel Eloquent withSum() and withCount() Example
- Laravel Eloquent Group By Example
- Laravel Eloquent Delete Record By ID Example
- PHP Laravel Inner Join Query Example
- Laravel Eloquent take() and skip() Query Example
- Laravel Eloquent whereNotBetween() Query Example
- Laravel Eloquent WhereNotIn Query Example
- Laravel Many to Many Eloquent Relationship Tutorial
- How to add LIKE query in Elasticsearch?