i am very new in Laravel,currently working with Laravel4.I am trying to add a multiple column search functionality in my project.I can do single column eloquent search query,But honestly i have no idea how to do multiple column eloquent search query in laravel.I have two drop down menu
我是Laravel的新手,目前正在与Laravel4合作。我正在尝试在我的项目中添加多列搜索功能。我可以做单列雄辩的搜索查询,但说实话我不知道怎么做多列滔滔不绝的搜索查询laravel。我有两个下拉菜单
1.Locatiom 2.blood group.
1.Locatiom 2.blood group。
i want to search an user having certain blood group against certain location.That is, user will select a location and blood group from those two drop down menu at a time and hit the search button.
我想在某个位置搜索具有特定血型的用户。也就是说,用户将一次从这两个下拉菜单中选择一个位置和血型并点击搜索按钮。
In my database,i have two column, one contains the location and another contains the blood group of a certain user. Now,what should be the eloquent query for such a search?
在我的数据库中,我有两列,一列包含位置,另一列包含某个用户的血型。现在,这种搜索的雄辩查询应该是什么?
2 个解决方案
#1
28
Simply chain where
for each field you need to search through:
只需链接您需要搜索的每个字段的位置:
// AND
$results = SomeModel::where('location', $location)->where('blood_group', $bloodGroup)->get();
// OR
$results = SomeModel::where('location', $location)->orWhere('blood_group', $bloodGroup)->get();
You can make it easier to work with thanks to the scopes:
由于范围的原因,您可以更轻松地使用它:
// SomeModel class
public function scopeSearchLocation($query, $location)
{
if ($location) $query->where('location', $location);
}
public function scopeSearchBloodGroup($query, $bloodGroup)
{
if ($bloodGroup) $query->where('blood_group', $bloodGroup);
}
// then
SomeModel::searchBloodGroup($bloodGroup)->searchLocation($location)->get();
Just a sensible example, adjust it to your needs.
只是一个明智的例子,根据您的需要进行调整。
#2
0
This is a great way but i thing there is an error somewhere as laravel would not allow you to access a non-static function so instead of using SomeModel::searchBloodGroup($bloodGroup)->searchLocation($location)->get();
you could simply use SomeModel::BloodGroup($bloodGroup)->Location($location)->get();
这是一个很好的方式,但我事情有一个错误,因为laravel不允许你访问非静态函数,所以而不是使用SomeModel :: searchBloodGroup($ bloodGroup) - > searchLocation($ location) - > get() ;你可以简单地使用SomeModel :: BloodGroup($ bloodGroup) - > Location($ location) - > get();
take not of the searchBloodGroup has been changed to BlooGroup, thats how you will use it for all others also
不要将searchBloodGroup更改为BlooGroup,这就是你将如何将它用于所有其他人
#1
28
Simply chain where
for each field you need to search through:
只需链接您需要搜索的每个字段的位置:
// AND
$results = SomeModel::where('location', $location)->where('blood_group', $bloodGroup)->get();
// OR
$results = SomeModel::where('location', $location)->orWhere('blood_group', $bloodGroup)->get();
You can make it easier to work with thanks to the scopes:
由于范围的原因,您可以更轻松地使用它:
// SomeModel class
public function scopeSearchLocation($query, $location)
{
if ($location) $query->where('location', $location);
}
public function scopeSearchBloodGroup($query, $bloodGroup)
{
if ($bloodGroup) $query->where('blood_group', $bloodGroup);
}
// then
SomeModel::searchBloodGroup($bloodGroup)->searchLocation($location)->get();
Just a sensible example, adjust it to your needs.
只是一个明智的例子,根据您的需要进行调整。
#2
0
This is a great way but i thing there is an error somewhere as laravel would not allow you to access a non-static function so instead of using SomeModel::searchBloodGroup($bloodGroup)->searchLocation($location)->get();
you could simply use SomeModel::BloodGroup($bloodGroup)->Location($location)->get();
这是一个很好的方式,但我事情有一个错误,因为laravel不允许你访问非静态函数,所以而不是使用SomeModel :: searchBloodGroup($ bloodGroup) - > searchLocation($ location) - > get() ;你可以简单地使用SomeModel :: BloodGroup($ bloodGroup) - > Location($ location) - > get();
take not of the searchBloodGroup has been changed to BlooGroup, thats how you will use it for all others also
不要将searchBloodGroup更改为BlooGroup,这就是你将如何将它用于所有其他人