Laravel5.7のCronJobを使用して、緯度と経度を郵便番号からデータベースに更新する方法

iAmGroot

ジョブ/イベントを使用して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'));
}
user11408553
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;         
    }

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事