简体   繁体   English

如何在未来在 Laravel 中的特定日期和时间发布博客文章

[英]How can I Publish A Blog Posts In The Future On A Specific Date And TIme In Laravel

I'm at the beginner level using the laravel PHP framework.我处于使用 laravel PHP 框架的初学者级别。 I worked on a blog web application, but I want to do some upgrade.我开发了一个博客网络应用程序,但我想做一些升级。

One of the upgrades is to be able to schedule posts to be published in the future on a selected date and time.其中一项升级是能够安排将来在选定的日期和时间发布帖子。 Like that of Facebook, or that of rainlab blog in October CMS.就像 Facebook 的那样,或者 10 月 CMS 中的rainlab 博客的那样。

I don't know how to go about this, I would really appreciate it if someone can help me out.我不知道该怎么做,如果有人可以帮助我,我将不胜感激。

The easiest way to implement delayed posting is to add publish date column (eg published_at) to posts table and retrieve posts where publish date < now.实现延迟发布的最简单方法是将发布日期列(例如已发布的_at)添加到帖子表并检索发布日期 < 现在的帖子。

Schema:架构:

$table->timestamp('published_at');

Retrieve example:检索示例:

$posts = Post
    ::where('published_at', '<', now())
    ->orderByDesc('published_at')
    ->paginate(50);
  1. The firstly i will create in database column like posted_at and show,that columns will be helpful later.首先我将在像posted_at这样的数据库列中创建并显示,这些列稍后会有所帮助。
  2. You should create command using您应该使用创建命令

php make:command MyCommand php make:command MyCommand

  1. Then in your app/console/command you will have your command然后在您的应用程序/控制台/命令中,您将拥有您的命令

  2. In app/console/kernel in在应用程序/控制台/内核中

protected variable $commands受保护的变量 $commands

register your command,put path注册您的命令,输入路径

  1. Inside your command using Eloquent or Db query get all posts where show=0 and posted_at在使用 Eloquent 或 Db 查询的命令中,获取 show=0 和 posts_at 的所有帖子

    $now=date("Ymd"); $data=DB::table('test')->where('show',0)->whereRaw("posted_at<$now")->get();
  2. Now you can use each loop and change show=1,something like that:现在您可以使用每个循环并更改 show=1,如下所示:

     $date->each(function ($item){ DB::table('test')->where('id',$item->id)->update(['show'=>1]); });

Last job is put in kernel code which will be run after 1m,try this ->最后一个工作放在内核代码中,它将在 1m 后运行,试试这个 ->

$schedule->command('myCommand')->everyMinute();

EDIT: So i checked my code i put changes so your command more or less should looks like this :编辑:所以我检查了我的代码并进行了更改,因此您的命令或多或少应该如下所示:

 $now=date("Y-m-d");
 $data=DB::table('test')->where('show_',0)->whereRaw("date(posted_at)<='$now'")->get();
 $data->each(function ($item){
 DB::table('test')->where('id',$item->id)->update(['show_'=>1]);

Remember to put in header of your command this if you use DB如果您使用 DB,请记住在命令的标题中放入

 Use DB;

if Eloquent this but you must change the DB to Model_name如果 Eloquent this 但您必须将 DB 更改为 Model_name

use App\Name_model;

And that is Kernel.php这就是 Kernel.php

protected $commands = [
        'App\Console\Commands\MyCommand',
    ];

// and 

protected function schedule(Schedule $schedule)
    {
        $schedule->command('My:Command')->everyMinute();
    }

I check if after 1min records in my test database was change ,and show_=0 changed to show_=1 and that's all我检查我的测试数据库中的记录是否在 1 分钟后更改,并且 show_=0 更改为 show_=1,仅此而已

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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