首页 / PHP / php-多个动态查询范围
php-多个动态查询范围
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php-多个动态查询范围,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1774字,纯文字阅读大概需要3分钟。
内容图文
我已经在数据库查询上建立了一个过滤器,现在
我想让它更舒适.
目标是使其具有动态性,这意味着
用户可以选择任意组合.
public function filter(FilterList $request)
{
$view = view('profile.browse.list');
$search = $request->get('filter');
$filter = [
'male' => Profile::male(),
'female' => Profile::female(),
'10' => Profile::man(),
'11' => Profile::women(),
'12' => Profile::couple(),
'15' => Profile::domina(),
'20' => Profile::affaire(),
'21' => Profile::cybersex(),
'22' => Profile::marriage(),
'23' => Profile::flirt(),
];
return $view->with([
'List' => $filter[$search]->unmoderated()->paginate(15)->appends(['filter' => $search]),
]);
}
我知道我的解决方案并不是很好的解决方案.
我已将查询范围保存在$filter数组中,
无法添加第二个或想要的范围
$users = User :: popular()-> women()-> orderBy(‘created_at’)-> get();
http://laravel.com/docs/5.0/eloquent#query-scopes
有任何暗示对我有帮助.
我不知道要开始…
谢谢你的帮助
编辑
public function scopeMarriage($query)
{
return $query->where('LookFor', 'LIKE', '%Ehe%');
}
解决方法:
因此,用户正在提交动态数量的输入,并希望基于这些输入将查询范围链接到您的查询?如果那是您想要的,我相信您可以做这样的事情:
// Method array taken from your example
$methods = [
'male' => 'male',
'female' => 'female',
'12' => 'couple',
];
// Begin the query. Since the unmoderated() scope query is always called,
// I used that in this example.
// But note that you can also use Model::query(); to begin a query
$query = Profile::unmoderated();
// Loop through the request inputs
foreach ($request->all() as $param)
{
// If it exists in the provided array, add it to the query.
if (array_key_exists($param, $methods))
{
$query->{$methods[$param]}();
}
}
// Call get, paginate, etc. to actually fetch the results and then return it.
return $query->paginate(15);
内容总结
以上是互联网集市为您收集整理的php-多个动态查询范围全部内容,希望文章能够帮你解决php-多个动态查询范围所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。