sandeep
sandeep

Reputation: 539

laravel query for delete where date < now() with INTERVAL N days

I am working on a laravel project now I stuck on a query. I want to use this query:

DELETE FROM on_search WHERE search_date < NOW() - INTERVAL N DAY

but cant converted into laravel like DB::table('table_name)->where(......)-> _____ ;

Upvotes: 1

Views: 2573

Answers (2)

Yada
Yada

Reputation: 31225

You can also do date manipulation in php with the Carbon class.

// Top of file.
use Carbon\Carbon;

DB::table('table_name)->where('search_date', '<', Carbon::now()->subDay(10));

Upvotes: 0

Nick
Nick

Reputation: 10143

$days = 10;

DB::table('on_search')
    ->whereRaw('search_date < NOW() - INTERVAL ? DAY', [$days])
    ->delete();

Upvotes: 1

Related Questions