Laravel与Paginator合并了2个系列 [英] Laravel merge 2 collection with paginator

查看:70
本文介绍了Laravel与Paginator合并了2个系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对两个不同的集合进行分页.有优先列表.放在优先级列表中的钱.

I need to do paging with two different collection. Have priority listing. Money that is placed in the priority list.

为什么不执行单个查询,您可以说出优先级列表.原因,使用过滤时;我选择的城市,县,类别.但是,站点所有者在城市,县甚至您想要的类别中列出的优先区域之一中具有优先列表.

Why do not you do a single query, you can say the priority list. Reason, when using filtering; city, county, category I choose. However, the site owner, having priority listing in the city, county, or even in one of the priority areas listed in the category you want.

因此,在使用3个条件的查询关键字中,其他条件在第一个条件中就足够了.每页列出20条记录,如果主要列表中有30条第一个列表,则我希望在另一个列表中列出第二条记录.

So in a query keywords when using 3 criteria, other criteria will be enough in the first. Each page lists 20 records, if the primary listing of the 30 first listing, the second collection of records in the other I would like to be listed thereafter.

根据通常的准则,现在我与您共享代码.等待您的建议.先感谢您.

According to the criteria normally be with you now I share my code. Waiting for your suggestions. Thank you in advance.

对不起,我的英语不好.

I'm sorry for my bad english.

DB::table('UserAds')
        ->join('Ads', 'Ads.id', '=', 'UserAds.ad_id')
        ->join('Users', 'Users.id', '=', 'UserAds.user_id')
        ->join('AdCategory', 'AdCategory.ad_id', '=', 'Ads.id')
        ->join('AdEducationPrices', 'AdEducationPrices.ad_id', '=', 'Ads.id')
        ->join('City', 'City.id', '=', 'Ads.city_id')
        ->join('Town', 'Town.id', '=', 'Ads.town_id')
        ->leftJoin('AdMedias', 'AdMedias.ad_id', '=', 'Ads.id')
        ->select('Ads.*', 'AdEducationPrices.*', 'City.name as city_name', 'City.id as city_id',
                                    'Town.name as town_name', 'Town.id as town_id', 'AdMedias.url')
        ->where('Ads.is_publish', 1)
        ->whereIn('AdCategory.category_id', $new_category_array)
        ->where('Ads.city_id', $city_id)
        ->where('Ads.town_id', $town_id)
        ->where('UserAds.is_purchased', true)
        ->where('UserAds.started_at', '<', date('Y-m-d H:i:s'))
        ->where('UserAds.finished_at', '>=', date('Y-m-d H:i:s'))
        ->where('AdMedias.type', 'picture')
        ->groupby('UserAds.ad_id')->orderby('finished_at', 'DESC')
        ->orderby('started_at', 'DESC')->paginate(20);

推荐答案

我将array_merge用于2个查询.并为分页器过滤记录.请阅读我的问题.我希望可以帮助某人

I used array_merge for 2 queries. And filtered records for paginator. Ty for reading my question. I hope to help someone

$new_collection = array_merge($Priority, $Ads);
$new_collection_count = count($new_collection);

$perPage = 20;
$currentPage = Input::get('page', 1) - 1;
$new_collection = array_slice($new_collection, $currentPage * $perPage, $perPage);
$Ads = Paginator::make($new_collection, $new_collection_count, $perPage);

这篇关于Laravel与Paginator合并了2个系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆