
Laravel delete all rows older than 30 days
You can delete all rows from table which are 30 days older from current date. You can use whereDate() and whereRaw() SQL query method to delete the records from table.
Answers 1
-
Laravel eloquent to delete 30 days older records from today's date
//1. whereRaw() SQL Query: $users = \App\Models\User::whereRaw('DATEDIFF(NOW(), created_at) > 30')->delete(); dd($users); //OR //2. whereDate() method $users = \App\Models\User::whereDate('created_at', '<=', now()->subDays(30))->delete(); dd($users);
0It will delete all the records which are 30 days older from today's date and return the number of records as output deleted from table. You can use any of them to get the 30 days older records from the particular table.
Random Code Snippet Queries: Laravel
- How to implement toggleLike() method in Overtrue\LaravelLike laravel package
- How to add a key value pair to existing array in laravel
- Route group with URI prefix using middleware and route name prefixes
- How to delete record in Laravel with ajax
- Use withCount() to get total number of records with relationship
- Link storage folder in laravel 8
- Laravel upload file with original file name
- RuntimeException You must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
- How to get selected categories on edit record with Select2
- Ajax GET request in laravel
- Database transactions in laravel
- Fatal error: Uncaught Error: Class "Illuminate\Foundation\Application" not found
- How to call Laravel route in jQuery
- Pagination in laravel
- Ignore Records where a field has NULL value in Laravel
- Trying to access array offset on value of type null error in laravel
- Display message with session flash using bootstrap alert class in laravel
- How to get random string in Laravel
- How to pass data to partial view file in laravel
- How to authenticate admin users in Laravel ?
- Get products with number of orders in Laravel
- How to check data inserted or deleted in pivot after toggle method
- How to avoid duplicate entries in pivot table in Laravel
- Get current month records in laravel 7/8
- How to upload image in laravel 8