djhru
djhru

Reputation: 336

Automatic delete date for posts in laravel 5.4

I want to use softdelete() method of Laravel in my app and I want to say soft-delete the post after 30 days of publishing date and time, currently I do not have any code in my app because I'm researching first to see if it's possible or not.

My question is how can I say that the post has to be delete in 30 days after publishing date?

Upvotes: 2

Views: 1854

Answers (2)

Sandeesh
Sandeesh

Reputation: 11906

Create a job or command and perform the operation in there. Setup the task scheduler and schedule the task to run your job or command once a day or hour depending on your desired speed of post removal.

https://laravel.com/docs/5.4/scheduling

You can also write the code directly in the scheduler if it's simple.

$schedule->call(function () {
    Post::where('published_at', '<=', Carbon::now()->subDays(30))->delete();
})->daily();

Be sure to enable soft deletes on your model.

Upvotes: 2

Ionut
Ionut

Reputation: 156

What you need is to use the Task Scheduler.

You'll need to manually add a single cronjob that actually triggers the scheduler in Laravel. After this you create a command that deletes those old posts.

This command gets scheduled to run how often you want and the scheduler will handle the every run of that scheduled command (see documentation).

Upvotes: 0

Related Questions