Laravel:如何从数据库中搜索多个值

Laravel:如何从数据库中搜索多个值,laravel,laravel-5.5,Laravel,Laravel 5.5,在我的搜索中有$categroy\u id-$country\u id-$city\u id。有一个表activities包含所有这些值 $activities = Activity::where('category_id', $category_id) ->orWhere('country_id', $country_id) ->orWhere('city_id', $cit

在我的搜索中有
$categroy\u id-$country\u id-$city\u id
。有一个表
activities
包含所有这些值

$activities = Activity::where('category_id', $category_id)
                           ->orWhere('country_id', $country_id)
                           ->orWhere('city_id', $city_id)
                           ->get();
我只是在我的控制器中实现一个函数,但它返回所有数据

$activities = Activity::where('category_id', $category_id)
                           ->orWhere('country_id', $country_id)
                           ->orWhere('city_id', $city_id)
                           ->get();
我的控制器功能代码:

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'));
}
$activities = Activity::where('category_id', $category_id)
                           ->orWhere('country_id', $country_id)
                           ->orWhere('city_id', $city_id)
                           ->get();

如何执行此操作?

使用多个where子句:

$activities = Activity::where('category_id', $category_id)
                           ->orWhere('country_id', $country_id)
                           ->orWhere('city_id', $city_id)
                           ->get();
查询生成器

// 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();
$activities = Activity::where('category_id', $category_id)
                           ->orWhere('country_id', $country_id)
                           ->orWhere('city_id', $city_id)
                           ->get();
雄辩的

// 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();
$activities = Activity::where('category_id', $category_id)
                           ->orWhere('country_id', $country_id)
                           ->orWhere('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'));
}
$activities = Activity::where('category_id', $category_id)
                           ->orWhere('country_id', $country_id)
                           ->orWhere('city_id', $city_id)
                           ->get();

使用多个where子句:

$activities = Activity::where('category_id', $category_id)
                           ->orWhere('country_id', $country_id)
                           ->orWhere('city_id', $city_id)
                           ->get();
查询生成器

// 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();
$activities = Activity::where('category_id', $category_id)
                           ->orWhere('country_id', $country_id)
                           ->orWhere('city_id', $city_id)
                           ->get();
雄辩的

// 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();
$activities = Activity::where('category_id', $category_id)
                           ->orWhere('country_id', $country_id)
                           ->orWhere('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'));
}
$activities = Activity::where('category_id', $category_id)
                           ->orWhere('country_id', $country_id)
                           ->orWhere('city_id', $city_id)
                           ->get();

你的错误在于使用了拉威尔模型提供的雄辩功能。调用
all()
函数时,它基本上会从表中执行
SELECT*,并将结果作为对象返回。如果需要筛选此查询或添加任何内容,将不再使用
all()
,而是使用
get()
作为最后一个函数

$activities = Activity::where('category_id', $category_id)
                           ->orWhere('country_id', $country_id)
                           ->orWhere('city_id', $city_id)
                           ->get();

至于在模型上设置此查询,您已经在模型中了!当您调用
Activity::whatever-function-next()
时,您就在模型内部,而Laravel已经为您提供了所有这些函数来轻松创建查询。第一个答案为您提供了所需的一切,只需确保了解这些函数和类实际上在做什么。

您的错误在于使用了Laravel模型提供的雄辩的函数。调用
all()
函数时,它基本上会从表中执行
SELECT*,并将结果作为对象返回。如果需要筛选此查询或添加任何内容,将不再使用
all()
,而是使用
get()
作为最后一个函数

$activities = Activity::where('category_id', $category_id)
                           ->orWhere('country_id', $country_id)
                           ->orWhere('city_id', $city_id)
                           ->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'));
}
$activities = Activity::where('category_id', $category_id)
                           ->orWhere('country_id', $country_id)
                           ->orWhere('city_id', $city_id)
                           ->get();

您可以使用此查询执行搜索,

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'));
}
$activities = Activity::where('category_id', $category_id)
                           ->orWhere('country_id', $country_id)
                           ->orWhere('city_id', $city_id)
                           ->get();

首先,您需要为活动表创建一个模型

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();
然后你可以像他上面说的那样写查询

$activities = Activity::where('category_id', $category_id)
                           ->orWhere('country_id', $country_id)
                           ->orWhere('city_id', $city_id)
                           ->get();

首先,您需要为活动表创建一个模型

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();
然后你可以像他上面说的那样写查询

$activities = Activity::where('category_id', $category_id)
                           ->orWhere('country_id', $country_id)
                           ->orWhere('city_id', $city_id)
                           ->get();

我想在模型上设置此查询?如何设置?如果没有
$categroy\u id-$country\u id-$city\u id
的另一个选项为空,则返回所有活动。如何设置。首先,您需要为活动表创建一个模型。类活动扩展了模型{protected$table='activities';//table name protected$guarded=[];},然后您可以像上面所说的那样编写查询$activities=Activity::where('category_id',$category_id)->或者where('country_id',$country_id)->或者where('city_id',$city_id)->get();我想在模型上设置这个查询?我怎么做?还有一个问题,如果没有
$categroy_id-$country_id-$city_id
则为null,返回所有活动。我如何设置。首先需要为活动表创建一个模型。类活动扩展了模型{protected$table='activities';//表名protected$guarded=[];}然后您可以像上面所说的那样编写查询$activities=Activity::where('category_id',$category_id)->或者where('country_id',$country_id)->或者where('city_id',$city_id)->get();