Laravel 合并两个集合并对其分页
所以我在数据库里有广告。 广告有三种类型:特色广告、粗体广告和免费广告。 我想在页面中显示它们。首先根据选择的类别。 我想显示所有特色的第一个添加日期,然后所有加粗和免费的项目添加日期。我想给它们编页码 我试过这个:Laravel 合并两个集合并对其分页,laravel,laravel-4,pagination,Laravel,Laravel 4,Pagination,所以我在数据库里有广告。 广告有三种类型:特色广告、粗体广告和免费广告。 我想在页面中显示它们。首先根据选择的类别。 我想显示所有特色的第一个添加日期,然后所有加粗和免费的项目添加日期。我想给它们编页码 我试过这个: $adsFeatured = Ads::where('category', '=', $category)->where('status', 'enabled')->where('type', 'featured')->get(); $adsOth
$adsFeatured = Ads::where('category', '=', $category)->where('status', 'enabled')->where('type', 'featured')->get();
$adsOther = Ads::where('category', '=', $category)->where('status', '=', 'enabled')->where('type', '=', 'free')->orWhere('type', '=', 'bold')->get();
$adsOther->merge($adsFeatured);
$adsFeatured->paginate(15);
当然,如果我使用get(),它就不能分页,但如果我不使用它,我就不能合并它们,我想合并它们,因为我希望它能将它们按那个顺序排列
谢谢你的帮助,我希望我已经把一切都解释清楚了。:) 您应该可以在一个查询中完成这项操作。试试这个
$ads = Ads::selectRaw('*, IF(type = "featured",1,0) AS is_featured')
->where('category', $category)
->where('status', 'enabled')
->orderBy('is_featured', 'desc')
->orderBy('created_at', 'desc')
->paginate(15);
它似乎得到了所有的广告,应该在类别,但我不能得到“$ad->id”或类似的东西。。如何访问字段?啊,很抱歉,您必须选择
*
以及其他字段。查看编辑后的答案非常感谢您的帮助这正是我所需要的:)