Laravel : 如何从数据库中搜索多个值
Laravel : how to search multiple value from database
在我的搜索中有 $categroy_id - $country_id - $city_id
。有一个 table activities
具有所有这些值。
我只是在我的控制器中实现了一个功能,但它 return 所有数据。
我的控制器功能码:
public function PlanActivity(Request $request){
$category_id = $request->category_id;
$countryid = $request->country_id;
$cityid = $request->city_id;
$listactivity = Activity::all(); // get all activity
if($category_id != '') {
$listactivity->where('category_id', function ($query) use ($category_id) {
$query->where('category_id', $category_id);
});
}
return view('front.plan_activity',compact('listactivity'));
}
我该怎么做?
使用多个 where 子句:
查询生成器
// match any one of the values
$activities = DB::table('activities')->where('category_id', $category_id)
->orWhere('country_id', $country_id)
->orWhere('city_id', $city_id)
->get();
// match all of the values
$activities = DB::table('activities')->where('category_id', $category_id)
->where('country_id', $country_id)
->where('city_id', $city_id)
->get();
Eloquent
// match any one of the values
$activities = Activity::where('category_id', $category_id)
->orWhere('country_id', $country_id)
->orWhere('city_id', $city_id)
->get();
// match all of the values
$activities = Activity::where('category_id', $category_id)
->where('country_id', $country_id)
->where('city_id', $city_id)
->get();
// this can be merged together in one array
$activities = Activity::where([
'category_id' => $category_id,
'country_id' => $country_id,
'city_id' => $city_id
])->get();
如果请求参数为空
public function PlanActivity(Request $request){
if (!$categroy_id && !$country_id && !$city_id) {
$activities = Activity::all();
} else {
// do the above queries
}
return view('front.plan_activity',compact('activities'));
}
您的错误在于使用了 Laravel 模型提供的 eloquent 功能。当您调用 all()
函数时,它基本上会将 SELECT * FROM
您的 table 和 returns 结果作为一个对象。如果您需要过滤此查询或添加任何内容,您将不再使用 all()
,而是使用 get()
作为最后一个函数。
至于在模型上设置此查询,您已经在模型中了!当您调用 Activity::whatever-function-next()
时,您就在模型内部,而 Laravel 已经为您提供了所有这些功能,可以轻松创建查询。第一个答案给了你所有你需要的,只要确保理解这些函数和 类 实际在做什么。
您可以使用此查询进行搜索,
public function PlanActivity(Request $request)
{
$category_id = $request->category_id;
$country_id = $request->country_id;
$city_id = $request->city_id;
$activity = new Activity; // get all activity
$search = $activity->search();
if($category_id) {
$search->where('category_id', $category_id);
}
if($country_id){
$search->where('country_id', $country_id);
}
if($city_id){
$search->where('city_id', $city_id);
}
$listactivity = $search->
return view('front.plan_activity',compact('listactivity'));
}
首先,您需要为 activity table 创建一个模型。
class Activity extends Model
{
protected $table = 'activities'; //table name
protected $guarded = [];
}
然后你可以像他上面说的那样写查询
$activities = Activity::where('category_id', $category_id)
->orWhere('country_id', $country_id)
->orWhere('city_id', $city_id)
->get();
在我的搜索中有 $categroy_id - $country_id - $city_id
。有一个 table activities
具有所有这些值。
我只是在我的控制器中实现了一个功能,但它 return 所有数据。
我的控制器功能码:
public function PlanActivity(Request $request){
$category_id = $request->category_id;
$countryid = $request->country_id;
$cityid = $request->city_id;
$listactivity = Activity::all(); // get all activity
if($category_id != '') {
$listactivity->where('category_id', function ($query) use ($category_id) {
$query->where('category_id', $category_id);
});
}
return view('front.plan_activity',compact('listactivity'));
}
我该怎么做?
使用多个 where 子句:
查询生成器
// match any one of the values
$activities = DB::table('activities')->where('category_id', $category_id)
->orWhere('country_id', $country_id)
->orWhere('city_id', $city_id)
->get();
// match all of the values
$activities = DB::table('activities')->where('category_id', $category_id)
->where('country_id', $country_id)
->where('city_id', $city_id)
->get();
Eloquent
// match any one of the values
$activities = Activity::where('category_id', $category_id)
->orWhere('country_id', $country_id)
->orWhere('city_id', $city_id)
->get();
// match all of the values
$activities = Activity::where('category_id', $category_id)
->where('country_id', $country_id)
->where('city_id', $city_id)
->get();
// this can be merged together in one array
$activities = Activity::where([
'category_id' => $category_id,
'country_id' => $country_id,
'city_id' => $city_id
])->get();
如果请求参数为空
public function PlanActivity(Request $request){
if (!$categroy_id && !$country_id && !$city_id) {
$activities = Activity::all();
} else {
// do the above queries
}
return view('front.plan_activity',compact('activities'));
}
您的错误在于使用了 Laravel 模型提供的 eloquent 功能。当您调用 all()
函数时,它基本上会将 SELECT * FROM
您的 table 和 returns 结果作为一个对象。如果您需要过滤此查询或添加任何内容,您将不再使用 all()
,而是使用 get()
作为最后一个函数。
至于在模型上设置此查询,您已经在模型中了!当您调用 Activity::whatever-function-next()
时,您就在模型内部,而 Laravel 已经为您提供了所有这些功能,可以轻松创建查询。第一个答案给了你所有你需要的,只要确保理解这些函数和 类 实际在做什么。
您可以使用此查询进行搜索,
public function PlanActivity(Request $request)
{
$category_id = $request->category_id;
$country_id = $request->country_id;
$city_id = $request->city_id;
$activity = new Activity; // get all activity
$search = $activity->search();
if($category_id) {
$search->where('category_id', $category_id);
}
if($country_id){
$search->where('country_id', $country_id);
}
if($city_id){
$search->where('city_id', $city_id);
}
$listactivity = $search->
return view('front.plan_activity',compact('listactivity'));
}
首先,您需要为 activity table 创建一个模型。
class Activity extends Model
{
protected $table = 'activities'; //table name
protected $guarded = [];
}
然后你可以像他上面说的那样写查询
$activities = Activity::where('category_id', $category_id)
->orWhere('country_id', $country_id)
->orWhere('city_id', $city_id)
->get();