Laravel cronjob在服务器上没有正确执行

时间:2017-10-16 10:30:09

标签: php laravel cron scheduler

我做了一个cronjob,每天都在 00:00 执行,它运行命令,我在Laravel check:date

了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

修改

计划资助模式

    <?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 '';
        }
    }

}

1 个答案:

答案 0 :(得分:0)

经过大量挖掘后我找到了答案。

php -d register_argc_argv=On /home/user/public_html/artisan check:date

Source.

感谢大家的帮助。 :)