简体   繁体   中英

Laravel commands do not save and update to database

I am writing a game simulator and want to create a cron job to run the game everyday. I am using Laravel's command and scheduler to do this.

The task is simple. I just want to pull a record from the database and update it every time the command runs. However I noticed the changes sometimes do not get saved to the database. I've tried debugging for the last 4 days but it seems very random and I do not see any patterns. I print the record before and after the update and it shows it was updated fine. But when I check my database, sometimes it shows no changes. Its as if sometimes the changes randomly revert after it was updated. But I get no error messages. Other than the TeamRecord records, I've tried updating other objects and have the same random reverting issue. Is there something I am missing? Below is the code for my Laravel command. I use php artisan games:run to run it.

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;
use App\Game;
use App\MainLine;
use App\GameLog;
use App\TeamRecord;

class RunGames extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'games:run';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // Get the record from the database
        $record1 = $g->homeTeam()->teamRecords->sortByDesc('season_number')->first();
        // This shows the current data
        $this->info($record1);

        // Update the record to the database
        TeamRecord::where('id', $record1->id)->update(['games_played' => $record1->games_played + 1]);
        // This always shows the data has changed. But when I check the database, sometimes the record did not change.
        $this->info(TeamRecord::find($record1->id));
    }

The cron user probably doesn't have write access to storage/logs/laravel.log and anything else it might need to write to.

Add

\Log::error('This definitely should have run!');

to the first line of handle() and see if it does have write access. You can also use this to debug it further.

I see you haven't passed $g to the function (or to the construct). Commands don't necessarily throw errors.

public function handle()
    {
        // Get the record from the database
        $record1 = $g->homeTeam()->teamRecords->sortByDesc('season_number')->first();
        // This shows the current data
        $this->info($record1);

        // Update the record to the database
        TeamRecord::where('id', $record1->id)->update(['games_played' => $record1->games_played + 1]);
        // This always shows the data has changed. But when I check the database, sometimes the record did not change.
        $this->info(TeamRecord::find($record1->id));
    }

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