首页 > 解决方案 > 如何在分页对象上使用 ->unique() 来删除 laravel 中的所有重复项

问题描述

我的 laravel 中有一个分页集合,如下所示:

$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(10);

现在,如果我->unique在我的上使用$data它只会返回我的第 10 个结果,不显示其他结果我不想从我的集合中删除分页,因为它会像 5k 记录一样调用很重的对象,我不想使用 join as只要它对我造成其他问题,所以我的问题是:是否有可能在 laravel 或资源中唯一的分页对象?

标签: phpmysqllaravel

解决方案


推荐阅读