How to Get Today Created Records in Laravel?
Today we will learn How to Get Today Created Records in Laravel?. In this tutorial, I will show you how to get today to create data in the Laravel controller. I will show you an example of Laravel getting today’s records. In this tutorial, we will step by step create examples about how to get records today in Laravel. we can get today-created records in the Laravel 8 application.
Here, I will give you an example of how to get today’s created records in Laravel. and we will use whereDate()
with Carbon\Carbon
Object in this example. We use Carbon::today() will return something like this: 2022-04-25T00:00:00.000000Z
, so we use Laravel Model::whereDate('created_at', Carbon::today())
will only return records created at.
Also read: How to Get Last Month Records in Laravel?
I created the posts table structure as below you can see.
posts table
Example
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Posts;
use Carbon\Carbon;
class HomeController extends Controller
{
public function index() {
$posts = Posts::select("*")->whereDate('created_at', Carbon::today())->get()->toArray();
echo '<pre>';
print_r($posts);
die;
}
}
Output
Also read: How to Get the Last 6 Month Data in Laravel?
Array
(
[0] => Array
(
[id] => 1
[title] => how to integrate passport api in laravel
[body] => how to integrate passport api in laravel
[slug] => how-to-integrate-passport-api-in-laravel
[created_at] => 2022-04-23T15:19:50.000000Z
[updated_at] => 2022-04-23T15:19:50.000000Z
)
[1] => Array
(
[id] => 2
[title] => export mysql database to sql file using php
[body] => export mysql database to sql file using php
[slug] => export-mysql-database-to-sql-file-using-php
[created_at] => 2022-04-23T15:19:50.000000Z
[updated_at] => 2022-04-23T15:19:50.000000Z
)
[2] => Array
(
[id] => 3
[title] => how to force redirect http to https using htaccess
[body] => how to force redirect http to https using htaccess,how to force redirect http to https using htaccess,how to force redirect http to https using htaccess
[slug] => how-to-force-redirect-http-to-https-using-htaccess
[created_at] => 2022-04-23T15:19:50.000000Z
[updated_at] => 2022-04-23T15:19:50.000000Z
)
)