如何使用CronJob将邮政编码从纬度和经度更新到数据库? #laravel 5.7

时间:2019-05-15 04:27:52

标签: php laravel

使用作业/事件创建cron脚本,以使用注册页面上提供的邮政编码更新用户位置详细信息,例如城市,州,经度,纬度等。

// app/console/commands/Zipcron.php
public function handle()
{
    try {
        $test = new User()
        $test->latitude = $latitude;
        $test->longitude = $longitude;
        $test->save();
        return $this->info('successfully added');

    } catch (exception $e) {
        return $this->warning('successfully added');
    }
}
// app/console/kernel.php
protected function schedule(Schedule $schedule)
{
    $schedule->command(Commands\ZipCron::class)->everyMinute()
        ->appendOutputTo(storage_path('logs/scheduler.log'));
}

使用作业/事件创建cron脚本,以使用注册页面上提供的邮政编码更新用户位置详细信息,例如城市,州,经度,纬度等。

1 个答案:

答案 0 :(得分:1)

public function handle()
    {      
        $trial = Trial::whereNull('lat')->whereNull('lng')->whereNull('address')->get();
        foreach ($trial as $tr)
        {
            $response = Geocode::make()->address($tr->zipcode);
            if ($response){
                $lat     = $response->latitude();
                $lng     = $response->longitude();  
                $city    = $response->raw()->address_components[1]->long_name;
                $state   = $response->raw()->address_components[2]->long_name;
                $address = $response->formattedAddress();
                echo $response->locationType();   
                DB::table('trial')->where('id', $tr->id)->update(['lat' => $lat, 'lng' => $lng, 'city' => $city, 'state' => $state, 'address' => $address]);          
            }
        }exit;         
    }