【发布时间】:2016-12-01 03:06:35
【问题描述】:
我最初有这个搜索词的解决方案:
Post::where('title', 'LIKE', '%'.$searched.'%')->orWhere('content', 'LIKE', '%'.$searched.'%')->paginate(100);
我想要什么:
我希望标题中包含术语的记录位于集合的顶部
我的临时解决方案:
很明显:
$posts1 = Post::where('title', 'LIKE', '%'.$searched.'%')->paginate(100);
$posts2 = Post::where('content', 'LIKE', '%'.$searched.'%')->paginate(100);
$object = $posts1->merge($posts2);
要解决的问题
$object 集合无法分页。我收到错误Method hasPages does not exist.。
有什么方法可以将分页功能保留在结果(排序和合并)集合上?
【问题讨论】:
-
为什么不改用
union?只需联合这 2 个选择查询 gyazo.com/a94c16f4e8683c7d0def54cde18c5899 -
同样的错误:
Method hasPages does not exist. -
您使用
union时出现错误? -
不,抱歉,贴错了。使用您的解决方案时,我得到
SQLSTATE[21000]: Cardinality violation: 1222 The used SELECT statements have a different number of columns (SQL: (select count(*) as aggregate fromposts` wherecontentLIKE %konf%) union (select * frompostswheretitleLIKE %konf%))` -
你能截屏(显示)你更新的代码吗?同样,当您联合查询时,它必须指定相同数量的列
标签: php laravel collections eloquent laravel-5.3