首页 > 解决方案 > Laravel - 如何让 Cron Job 在我的本地主机 Windows 上工作

问题描述

我正在使用 Guzzle 使用外部 API。在我最终将其移动到服务器之前,我想将其保存到本地主机上的数据库中。

本地主机:8888/myapp

它适用于 Postman,当我使用 dd(); 时我可以看到数据;

控制台\命令\UpdateCreateEmployee

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Employee;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Auth;
use Exception;
use GuzzleHttp\Client; 
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;


class UpdateCreateEmployee extends Command
{

    protected $signature = 'command:UpdateCreateEmployee';

    protected $description = 'Update or Create Employee Profile';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {

        $userCompany = Auth::user()->company_id;        

        $client = new Client();
    $res = $client->request('GET','https://api.apptest.net/staff', [
       'query' => ['key' => 'wwwwwwdddd']
    ])->getBody();

       $clientdatas = json_decode($res->getContents(), true);        

    foreach($clientdatas as $clientdata)
    {

        $employee = HrEmployee::updateOrCreate([
            'employee_code' => $clientdata->staff_id,
        ],
        [
            'username'                              => strtok($request->email_company, '@'),
            'first_name'                            => $clientdata->first_name,
            'last_name'                             => $clientdata->flast_name,
            'other_name'                            => $clientdata->middle_name,
            'date_of_birth'                         => Carbon::parse($clientdata['date_of_birth'])->toDateString(),
            'hr_status'                             => $clientdata->hr_status,
            'address'                               => $clientdata->address,
            'company_id'                            => 1,
            'country_name'                          => $clientdata->country,
            'email'                                 => $clientdata->email,

        ]);    

        //user
        $user = User::updateOrCreate([
            'email'                                 => $employee->email,

        ],
        [
            'username'                              => $employee->username,
            'password'                              => bcrypt("123456"),
            'first_name'                            => $employee->first_name,
            'last_name'                             => $employee->last_name,
        ]);

         $user->assignRole('Employee');

         $employee->update(['user_id' => $user->id]);        

    }
 } 

}

核心

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{

    protected $commands = [
    'App\Console\Commands\UpdateCreateEmployee',
   ];

   protected function schedule(Schedule $schedule)
   {      
      $schedule->command('command:UpdateCreateEmployee')
              ->everyFifteenMinutes();          
   }

   protected function commands()
   {
      $this->load(__DIR__.'/Commands');

      require base_path('routes/console.php');
   }
}

十五分钟后,我检查了数据库,那里什么都没有。

如何使它在我的本地主机窗口上工作?

谢谢

标签: laravel

解决方案


在 Windows 环境下使用 cron 作业的原生方式是任务计划程序。每分钟运行一个新任务:

Program script: path/to/your/php.exe

Add arguments: path/to/your/artisan schedule:run

更多信息可以在这里找到:https ://quantizd.com/how-to-use-laravel-task-scheduler-on-windows-10/


推荐阅读