在Laravel 4中搜索和过滤/优化数据库结果 [英] Searching and filtering / refining database results in Laravel 4

查看:134
本文介绍了在Laravel 4中搜索和过滤/优化数据库结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种通过Laravel 4中的查询字符串搜索和过滤数据库表/雄辩模型的方法.

I'm looking for a way to search and filter a database table / Eloquent model through the query string in Laravel 4.

我有一个名为houses的表,其中的列名为:pricenamehasCoffeeMachinehasStove

I have a table named houses with the columns named: price, name, hasCoffeeMachineand hasStove

我希望用户能够执行以下操作: http://example.com?name=test&hasCoffeeMachine = 1

I want the user to be able to do something like: http://example.com?name=test&hasCoffeeMachine=1

这将获得名称为"test"且具有咖啡机的所有行.

Which will get all the rows where the name is "test" and they have a coffee machine.

我希望用户能够根据他们想要的过滤器添加URL参数.解释所有可能的查询字符串组合以从表中返回正确结果的最佳方法是什么?

I want the user to be able to add URL parameters based on the filter they desire. What is the best way to account for all the possible combinations of query strings to return the correct results from the table?

推荐答案

$houses = new House;

if(Input::get('name'))
    $houses->where('name', '=', Input::get('name'));

if(Input::get('hasCoffeeMachine'))
    $houses->where('hasCoffeeMachine', '=', Input::get('hasCoffeeMachine'));

// Keep adding more for every filter you have

// Don't do this till you are done adding filters.
$houses = $houses->get();

这篇关于在Laravel 4中搜索和过滤/优化数据库结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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