duangan6731 2019-05-15 04:27
浏览 38
已采纳

如何使用Laravel 5.7中的Cron Job将纬度和经度从zipcode更新到数据库

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'));
}
  • 写回答

1条回答 默认 最新

  • dongzhong1891 2019-06-04 09:37
    关注
    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;         
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题