简体   繁体   中英

Eloquent query with subquery

Since three days I'm working on following problem:

 protected function getTableQuery(): Builder
    {
        return User::
            where('age','>',21)
            ->AppendDistanceTo(
                $this->city->geo->long,
                $this->city->geo->lat,
                **lo**,
                **la**,
            );
        }

The Model User has user_id and a id for the city city_id.

In the model City there is longitude and latitude for each city_id. How can I get the longitude and latitude in the query above, für lo and la from the related Model?

Thank you very much for helping!

I want to show the distance from a selected city ( $this->city->geo->long,$this->city->geo->lat)

to the city, where the user lives.

Given your User model has a city relationship, and the City model has a geo relationship, you could eager load the city relation like so:

User::where('age', '>', 21)
->with('city.geo')

Then, $user->city->geo->long

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