Prem Basnet
Prem Basnet

Reputation: 47

How to soft delete data automatically after 35 days in laravel 5.6?

I have a table death_notice and notice_time. In notice_time table, there is a column named notice_duration and 35 is stored there which means that the death notice is displayed for 35 days in the website.

Now, I want to soft delete the data that crossed 35 days automatically in laravel 5.6. How can i do that?

Upvotes: 0

Views: 1006

Answers (5)

amin mahmoudi
amin mahmoudi

Reputation: 660

you can create laravel scheduling that run every night and execute below query:

App\deathnotice::withTrashed()
            ->where('created_at', '>=', deathnotice::now()->subDays($noticetime->notice_duration)->toDateTimeString());
            ->get();

Upvotes: 1

Madhuri Patel
Madhuri Patel

Reputation: 1270

you can use below code.It may be help you

$noticetime= App\noticetime::find(1);


$deathnotice= App\deathnotice::withTrashed()
                ->where('created_at', '>=', deathnotice::now()->subDays($noticetime->notice_duration)->toDateTimeString());
                ->get();

Upvotes: 0

Thanh Nguyen
Thanh Nguyen

Reputation: 410

Please use Laravel`s Schedule to make a crontab to delete. https://laravel.com/docs/5.7/scheduling

Upvotes: 1

PHP Geek
PHP Geek

Reputation: 4033

You can run cron job and check if created_date is greater than 35 days then change the status 0.

Upvotes: 0

TranDuc
TranDuc

Reputation: 23

Create a function to check the current time - created_at column, if> 35 days delete ..., Can use Scheduling https://laravel.com/docs/5.6/scheduling

Upvotes: 0

Related Questions