You probably need the distinct method.
https://laravel.com/docs/6.x/queries#selects
Remember eloquent has all query builder methods available also.
hi , i am using spatie laravel query builder to make some filters for my project and i have the code as below :
public function scopeFilter()
{
$data = QueryBuilder::for(Accommodation::class)
->allowedFilters([
AllowedFilter::scope('bed_count'),
AllowedFilter::scope('filter_price'),
AllowedFilter::scope('filter_date'),
AllowedFilter::scope('discounts'),
AllowedFilter::scope('name'),
AllowedFilter::exact('grade_stars'),
AllowedFilter::exact('city_id'),
AllowedFilter::exact('is_recommended'),
AllowedFilter::exact('accommodation_type_id'),
AllowedFilter::scope('accommodation_facility')
// 'name',
])
->allowedAppends(['cheapestroom'])
->allowedIncludes(['gallery','city','accommodationRooms','accommodationRooms.roomPricingHistorySearch','discounts','prices'])
->allowedSorts([
AllowedSort::custom('discount', new DiscountSort() ,'amount'),
AllowedSort::custom('price', new PriceSort() ,'price'),
])
->paginate(12);
return FilterResource::collection($data);
now i have 1 sort and 2 filters which is so important for me here is the code for them
$data = $query->join('accommodation_rooms as cr', 'accommodations.id', '=', 'cr.accommodation_id')
->join('room_pricing_histories as dr','cr.id', '=', 'room_pricing_histories.accommodation_room_id')
// ->select('cr.id')
->orderBy('dr.sales_price', 'desc')
->select('dr.sales_price', 'accommodations.*')
->groupBy('dr.sales_price','accommodations.id');
$direction = $descending ? 'DESC' : 'ASC';
return $data;
public function scopeFilterPrice(Builder $query, $start_price, $end_price): Builder
{
$data = $query->leftjoin('accommodation_rooms as ar','ar.id','=','accommodations.id')
->leftjoin('room_pricing_histories','room_pricing_histories.id','=','ar.id')
->select('accommodations.*')
->where('room_pricing_histories.sales_price', '>', $start_price)
->where('room_pricing_histories.sales_price', '<', $end_price);
return $data;
}
#secound filter as scope too
public function scopeFilterDate(Builder $query,$from_date,$to_date): Builder{
$data = $query->leftjoin('accommodation_rooms as br','br.id','=','accommodations.id')
->Join('room_capacity_histories','room_capacity_histories.id','=','br.id')
->select('accommodations.*')
->whereDate('room_capacity_histories.from_date', '>', $from_date)
->whereDate('room_capacity_histories.to_date', '<', $to_date);
return $data;
but this will bring me duplicate data no matter what groupby or other thing i use i want to know if there is any possibilty for me to remove duplicates on resource or some where else.
i did use ->unique() but it wont have the structure of pagination any more which cause me a problem so i want the structure of pagination first . here is the raw query :
"select `dr`.`sales_price`, `accommodations`.* from `accommodations` left join `accommodation_rooms` as `ar` on `ar`.`id` = `accommodations`.`id` left join `room_pricing_histories` on `room_pricing_histories`.`id` = `ar`.`id` left join `accommodation_rooms` as `br` on `br`.`id` = `accommodations`.`id` inner join `room_capacity_histories` on `room_capacity_histories`.`id` = `br`.`id` inner join `accommodation_rooms` as `cr` on `accommodations`.`id` = `cr`.`accommodation_id` inner join `room_pricing_histories` as `dr` on `cr`.`id` = `room_pricing_histories`.`accommodation_room_id` where `room_pricing_histories`.`sales_price` > ? and `room_pricing_histories`.`sales_price` < ? and date(`room_capacity_histories`.`from_date`) > ? and date(`room_capacity_histories`.`to_date`) < ? group by `dr`.`sales_price`, `accommodations`.`id` order by `dr`.`sales_price` desc"
thanks
Please or to participate in this conversation.