использовать условие if в запросе, когда значение равно нулю - PullRequest
0 голосов
/ 17 мая 2019

В запросе SQL я хочу добавить оператор if. если широта и долгота в базе данных равны нулю, запрос будет выполнен и заполнит значение в базе данных

$users = DB::table('users',IF ($user->lat && $user->lng == Null))->get();

foreach ($users as $user)
{
    $response = Geocode::make()->address($user->zipcode);

    if ($response) {
        $lat =   $response->latitude();
        $lng =  $response->longitude();
        $city =   $response->formattedAddress();
        echo  $response->locationType();

        DB::table('users')
            ->where('id', $user->id)
            ->update(['lat' => $lat,'lng' => $lng,'city' => $city]);

    }
}

Ответы [ 2 ]

4 голосов
/ 17 мая 2019
$users = User::whereNull('lat')->whereNull('lng')->get();

foreach ($users as $user) {
    $response = Geocode::make()->address($user->zipcode);

    if ($response) {
        $lat = $response->latitude();
        $lng = $response->longitude();
        $city = $response->formattedAddress();
        echo  $response->locationType();

        $user->update(['lat' => $lat,'lng' => $lng,'city' => $city]);
    }
}

Более подробная информация:

$city = $response->raw()->address_components[1]['long_name'];
$state = $response->raw()->address_components[2]['long_name'];
$country = $response->raw()->address_components[3]['long_name'];
1 голос
/ 17 мая 2019
 DB::table('users')->where([
    ['users.lat', '=', NULL],
    ['users.lng', '=', NULL])->get();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...