Php 如何使用where子句仅获取搜索的数据? $data['ads']=PostAd::where('category_id',$id) ->orwhere('district_id'、'LIKE'、'%'.$location.'%')) ->或其中('condition'、'LIKE'、'%'.$condition'.%')) ->或其中('价格','>='。$min_价格) ->或where('price','

Php 如何使用where子句仅获取搜索的数据? $data['ads']=PostAd::where('category_id',$id) ->orwhere('district_id'、'LIKE'、'%'.$location.'%')) ->或其中('condition'、'LIKE'、'%'.$condition'.%')) ->或其中('价格','>='。$min_价格) ->或where('price',',php,laravel,laravel-5,eloquent,Php,Laravel,Laravel 5,Eloquent,尝试类似的方法,根据选择的搜索参数选择性地添加条件 $data['ads'] = PostAd::where('category_id',$id) ->orwhere('district_id','LIKE','%'.$location.'%') ->orWhere('condition','LIKE','%'.$condition.'%') ->orWhere('price','>='.$min_pr

尝试类似的方法,根据选择的搜索参数选择性地添加条件


    $data['ads'] = PostAd::where('category_id',$id)      
        ->orwhere('district_id','LIKE','%'.$location.'%')
        ->orWhere('condition','LIKE','%'.$condition.'%')
        ->orWhere('price','>='.$min_price)
        ->orWhere('price','<='.$max_price)   
        ->orWhere('fuel',$fuel)
        ->orWhere('anchalorpradesh',$anchal)
        ->orWhere('mileage',$mileage)
        ->orWhere('kilometers',$kilometers)
        ->orWhere('engine',$engine) 
        ->get();

尝试类似的方法,根据搜索参数选择添加条件


    $data['ads'] = PostAd::where('category_id',$id)      
        ->orwhere('district_id','LIKE','%'.$location.'%')
        ->orWhere('condition','LIKE','%'.$condition.'%')
        ->orWhere('price','>='.$min_price)
        ->orWhere('price','<='.$max_price)   
        ->orWhere('fuel',$fuel)
        ->orWhere('anchalorpradesh',$anchal)
        ->orWhere('mileage',$mileage)
        ->orWhere('kilometers',$kilometers)
        ->orWhere('engine',$engine) 
        ->get();

根据通过“真值”测试的值,可以使用
when
方法有条件地向查询中添加子句:

PostAd::query()
->当($request->get('category_id')时,函数($query,$categoryId){
$query->where('category_id','=',$categoryId);
})
->分页();
作为第二个参数传递的闭包将接收两个参数:可以修改的查询生成器实例,以及作为第一个参数传递给
when
方法的值


您还可以更进一步,将过滤逻辑移动到专用类:

类后过滤器
{
受保护的美元请求;
受保护的$builder;
公共函数构造(请求$Request)
{
$this->request=$request;
}
公共功能应用(生成器$Builder)
{
$this->builder=$builder;
foreach($this->request->query()作为$key=>$value){
//将类似“category_id”的内容转换为“filterByCategoryId”`
$methodName='filterBy'.Str::studly($key);
如果(方法_存在($this$methodName)){
//如果该方法存在,则调用它
调用用户函数([$this$methodName],$value);
}
}
//返回修改后的查询生成器
返回$this->builder;
}
私有函数filterByCategoryId($value)
{
$this->builder->where('category_id','=',$value);
}
专用功能过滤器测风仪($value)
{
$this->builder->where('km','=',$value);
}
//等等。。。
}

类PostAd扩展模型
{
公共函数scopeFilters(生成器$query、PostAdFilters$filters)
{
返回$filters->apply($query);
}
}
然后,可以将此类注入控制器方法,并将其应用于模型:

公共函数搜索(PostAdFilters$filters)
{
返回PostAd::filter($filters)->paginate();
}

此方法基于您可以使用
when
方法根据通过“真值”测试的值有条件地向查询中添加子句:

PostAd::query()
->当($request->get('category_id')时,函数($query,$categoryId){
$query->where('category_id','=',$categoryId);
})
->分页();
作为第二个参数传递的闭包将接收两个参数:可以修改的查询生成器实例,以及作为第一个参数传递给
when
方法的值


您还可以更进一步,将过滤逻辑移动到专用类:

类后过滤器
{
受保护的美元请求;
受保护的$builder;
公共函数构造(请求$Request)
{
$this->request=$request;
}
公共功能应用(生成器$Builder)
{
$this->builder=$builder;
foreach($this->request->query()作为$key=>$value){
//将类似“category_id”的内容转换为“filterByCategoryId”`
$methodName='filterBy'.Str::studly($key);
如果(方法_存在($this$methodName)){
//如果该方法存在,则调用它
调用用户函数([$this$methodName],$value);
}
}
//返回修改后的查询生成器
返回$this->builder;
}
私有函数filterByCategoryId($value)
{
$this->builder->where('category_id','=',$value);
}
专用功能过滤器测风仪($value)
{
$this->builder->where('km','=',$value);
}
//等等。。。
}

类PostAd扩展模型
{
公共函数scopeFilters(生成器$query、PostAdFilters$filters)
{
返回$filters->apply($query);
}
}
然后,可以将此类注入控制器方法,并将其应用于模型:

公共函数搜索(PostAdFilters$filters)
{
返回PostAd::filter($filters)->paginate();
}
这种方法基于