简体   繁体   中英

How to update latitude and longitude from zipcode to the database using Cron Job in Laravel 5.7

Create a cron script using job/events to update user location details like city, state, longitude, latitude, etc. using the zip code provided on the registration page.

// 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'));
}
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;         
    }

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