简体   繁体   中英

Laravel cronjob not executing properly on server

I made a cronjob which executes every day at 00:00 , it runs a command which I made in 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']);
}
}

The command runs a query which checks for programme fundings which have the status 'open', checks their date and then proceeds to change their status to 'closed' if the 'date_end' value is less than today's date.

When the cronjob executes I get this error :

[ErrorException]


 Invalid argument supplied for foreach()

Does anyone know a solution? This is my command on the server:

php /home/user/public_html/artisan check:date

EDIT

Programmefunding model

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

}

I found the answer after a lot of digging.

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

Source.

Thanks everyone for the help. :)

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