How to use 'having' with paginate on relationship's column in laravel 5

前端 未结 5 477
耶瑟儿~
耶瑟儿~ 2020-12-10 14:22

I need to grab the vehicles whose relation \'dealer\' is having distance < 200

Vehicle::join(\'dealers\', \'vehicles.dealer_id\', \'=\', \'dealers.id\')
          


        
5条回答
  •  抹茶落季
    2020-12-10 15:00

    having and havingRaw doesn't have access to generated fields in the query see this example from laravel docs

    Your query will have to lookk like this

    Vehicle::join('dealers', 'vehicles.dealer_id', '=', 'dealers.id')
                ->select(DB::raw("dealers.id, ( cos( radians(latitude) ) * cos( radians( longitude ) ) ) AS distance"))
                ->havingRaw('(cos(radians(latitude)) * cos(radians(longitude))) < 200');
    

提交回复
热议问题