Laravel的cronjob无法正常运行在服务器上
问题描述:
我在00:00由它执行每天一个cronjob,它运行命令我在Laravel check:date
制造。Laravel的cronjob无法正常运行在服务器上
了checkdate
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Programmefunding;
use Carbon\Carbon;
class checkDate extends Command{
protected $signature = 'check:date';
protected $description = 'Check if date is valid';
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
Programmefunding::where('date_end', '<', Carbon::now())
->where('status', 'open')
->update(['status' => 'closed']);
}
}
该命令运行查询,检查对于具有状态“开放”程序拨款,,检查它们的日期,然后如果“DATE_END前进到改变它们的状态,以“封闭” '价值低于今天的日期。
当的cronjob执行我得到这个错误:
[ErrorException]
Invalid argument supplied for foreach()
有谁知道一个解决方案吗? 这是我的服务器上的命令:
php /home/user/public_html/artisan check:date
编辑
Programmefunding模式
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\SoftDeletes;
class Programmefunding extends Model
{
use SoftDeletes;
protected $fillable = ['status', 'name', 'description', 'permalink', 'date_start', 'date_end', 'why_end'];
public static $enum_status = ["draft" => "Draft", "open" => "Opened", "closed" => "Closed", "announced" => "Announced", "deferred" => "Deferred"];
/**
* Set attribute to date format
* @param $input
*/
public function setDateStartAttribute($input)
{
if ($input != null && $input != '') {
$this->attributes['date_start'] = Carbon::createFromFormat(config('app.date_format') . ' H:i:s', $input)->format('Y-m-d H:i:s');
} else {
$this->attributes['date_start'] = null;
}
}
/**
* Get attribute from date format
* @param $input
*
* @return string
*/
public function getDateStartAttribute($input)
{
$zeroDate = str_replace(['Y', 'm', 'd'], ['0000', '00', '00'], config('app.date_format') . ' H:i:s');
if ($input != $zeroDate && $input != null) {
return Carbon::createFromFormat('Y-m-d H:i:s', $input)->format(config('app.date_format') . ' H:i:s');
} else {
return '';
}
}
/**
* Set attribute to date format
* @param $input
*/
public function setDateEndAttribute($input)
{
if ($input != null && $input != '') {
$this->attributes['date_end'] = Carbon::createFromFormat(config('app.date_format') . ' H:i:s', $input)->format('Y-m-d H:i:s');
} else {
$this->attributes['date_end'] = null;
}
}
/**
* Get attribute from date format
* @param $input
*
* @return string
*/
public function getDateEndAttribute($input)
{
$zeroDate = str_replace(['Y', 'm', 'd'], ['0000', '00', '00'], config('app.date_format') . ' H:i:s');
if ($input != $zeroDate && $input != null) {
return Carbon::createFromFormat('Y-m-d H:i:s', $input)->format(config('app.date_format') . ' H:i:s');
} else {
return '';
}
}
}
答
我找到答案了很多挖后。
php -d register_argc_argv=On /home/user/public_html/artisan check:date
感谢大家的帮助。 :)
你能告诉我们'Programmefunding'模型吗? – ventaquil
它已被添加:)值得注意的是,'php artisan check:date'在通过cmd在本地运行时起作用。 – Jawee
db上的表名是否正确?不打印错误行? – LorenzoBerti