我在使用laravel PHP框架的初学者级别。我在博客Web应用程序上工作,但我想进行一些升级。
升级之一是能够安排在将来选定的日期和时间发布的帖子。就像Facebook或10月CMS上的rainlab博客一样。
我不知道该怎么办,如果有人可以帮助我,我将不胜感激。
实施延迟发布的最简单方法是在发布表中添加发布日期列(例如,published_at),并检索发布日期为
模式:
$table->timestamp('published_at');
检索示例:
$posts = Post
::where('published_at','<',now())
->orderByDesc('published_at')
->paginate(50)
,
php make:command MyCommand
然后在您的应用程序/控制台/命令中,您将拥有命令
在应用程序/控制台/内核中
受保护的变量$ commands
注册您的命令,放置路径
在命令中使用口才或db查询获取show = 0和posted_at的所有帖子
$now=date("Y-m-d");
$data=DB::table('test')->where('show',0)->whereRaw("posted_at<$now")->get();
现在您可以使用每个循环并更改show = 1了,诸如此类:
$date->each(function ($item){
DB::table('test')->where('id',$item->id)->update(['show'=>1]);
});
最后一项工作放在将在1m后运行的内核代码中,请尝试此操作->
$schedule->command('myCommand')->everyMinute();
编辑: 所以我检查了代码,然后进行了更改,所以您的命令或多或少应该看起来像这样:
$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]);
如果使用DB,请记住要在此命令的标题中放置
Use DB;
如果雄辩,但必须将数据库更改为Model_name
use App\Name_model;
那就是Kernel.php
protected $commands = [
'App\Console\Commands\MyCommand',];
// and
protected function schedule(Schedule $schedule)
{
$schedule->command('My:Command')->everyMinute();
}
我检查1分钟后测试数据库中的记录是否已更改,并且show_ = 0更改为show_ = 1,仅此而已