在 Laravel 中加入 leftJoin 查询

Join to leftJoin queries in Laravel

我有一个正在尝试检索的查询。它应该获得一个列表信息及其行程和位置详细信息。

这就是我在 Destinations 控制器中调用查询的方式:

public function destinations($id) {

        $destination = Destination::findOrFail($id);

        $listingGuides = Listing::findGuidesTrips($destination)
            ->with('locations')
            ->withCount('trips')
            ->get(); 

         return view('destinations.index', compact('listingGuides');

}

并且 findGuidesTrips 方法在列表模型中:

 public static function findGuidesTrips($destination) {

        $query = self::query()
            ->leftJoin('trips', 'listing_id', '=', 'listings.id')
            ->addSelect(
                \DB::raw('listings.name,listings.slug,listings.type,listings.id,MIN(trips.cost) as starting_price')
            )
            ->groupBy('listings.id');

        $query = self::query()
            ->leftJoin('locations', 'listing_id', '=', 'listings.id')
            ->addSelect(
                \DB::raw('locations.longitude as longitude')
            )->addSelect(
                \DB::raw('locations.latitude as latitude')
            );

        $query = $query->whereHas('locations',function($query) use ($destination) {
            $query->where('region', 'like', $destination->location)->orWhere('country', $destination->location);

        });

        return $query;
    }

这是我得到的:

如您所见,我有 2 个 $query = self::query() 查询,但只有一个被调用(最下面的一个)。它忽略了顶部 self::query。

我只是想知道如何将这 2 个 leftJoin 查询合并为一个查询?或者是否有更好的方法来执行此查询?

(我试过这样做:)

$query = self::query()
            ->leftJoin('trips', 'listing_id', '=', 'listings.id')
            ->addSelect(
                \DB::raw('listings.name,listings.slug,listings.type,listings.id,MIN(trips.cost) as starting_price')
            )
            ->leftJoin('locations', 'listing_id', '=', 'listings.id')
            ->addSelect(
                \DB::raw('locations.longitude as longitude')
            )->addSelect(
                \DB::raw('locations.latitude as latitude')
            )->groupBy('listings.id');

但它给我 Integrity constraint violation: 1052 Column 'listing_id' in on clause is ambiguous 错误

正如@Tim Lewis 和@Niklesh 所说,我所要做的就是:

trips.listing_id 用于第一个查询,locations.listing_id 用于第二个。

这是最后的查询:

public static function findGuidesTrips($destination) {

        $query = self::query()
            ->leftJoin('trips', 'trips.listing_id', '=', 'listings.id')
            ->addSelect(
                \DB::raw('listings.name,listings.slug,listings.type,listings.id,MIN(trips.cost) as starting_price')
            )
            ->leftJoin('locations', 'locations.listing_id', '=', 'listings.id')
            ->addSelect(
                \DB::raw('locations.longitude as longitude')
            )->addSelect(
                \DB::raw('locations.latitude as latitude')
            )->groupBy('listings.id');

        $query = $query->whereHas('locations',function($query) use ($destination) {
            $query->where('region', 'like', $destination->location)->orWhere('country', $destination->location);

        });

        return $query;
    }