简体   繁体   中英

Publish created post on a certain date

Right now I have a working post create function that creates a post and sends a notification to the user that a post has been created. How do I manage this if the author of the post wants to publish the post at a certain date. Let's say they fill in a publish_date and i want the controller to create the post on that publish_date. Right now it just creates the post and send notification instantly.

public function create()
{
  $post = Post::create([
     'title'=>$request->title,
     'details'=>$request->details,
     'publish_date'=> $request->publish_date
  ]);    
  User::get()->each->notify(new PostCreated($post)); //sends notification to user using queue and redis
}
  1. Alter your post table, add columns: publish_date and status . The first column will include the date (DATETIME) when you need to publish the post the second one - will include the status (ENUM or SMALLINT) of the post. You also can make this column like boolean in name it something like is_published . But my variant is more universal if you'll need to add more post statuses in the future.

  2. Schedule command which will check the date in the post DB record, compare with the current date, and publish it.

  3. Always wrap your create method with try/catch.

  4. Remember about single responsibility. Your create method should contain only create functionality. If you want to send notifications, use Events and Event Listeners for this. If you'll bind notification sending with an event listener, you'll escape repeating code in the scheduled command from point 2 of this list. Your event listener will listen the save event and handle a job with notification sending

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM