过滤器在laravel5.4
问题描述:
万一我有过滤器的用户与地点和日期,我已经写了查询像下面过滤器在laravel5.4
public function searchConsultants()
{
$location = $request->get('location');
$fromDate = $request->get('from_date');
$toDate = $request->get('to_date');
$data = DB::table('consultants')
->selectRaw('AVG(COALESCE(ratings.rating_for_manager, 0))as avg_rating, consultants.id,consultants.cunsultant_name,contact_number,location')
->where('location','LIKE','%'.$location.'%')
->whereBetween('date',[$fromDate,$toDate])
->leftJoin('ratings', 'ratings.consultant_id', 'consultants.id')
->groupBy('consultants.id')
->orderBy('avg_rating', 'DESC')
->get();
}
有了上面的查询,我可以得到的数据,但有时我不想带日期搜索我想只搜索位置,
上述查询中的问题我必须输入位置和日期来过滤用户,那么如何过滤只有位置或日期。
答
使用orWhere在https://laravel.com/docs/5.4/queries#where-clauses
public function searchConsultants()
{
$location = $request->get('location');
$fromDate = $request->get('from_date');
$toDate = $request->get('to_date');
$data = DB::table('consultants')
->selectRaw('AVG(COALESCE(ratings.rating_for_manager, 0))as avg_rating, consultants.id,consultants.cunsultant_name,contact_number,location')
->where('location','LIKE','%'.$location.'%')
->orwhereBetween('date',[$fromDate,$toDate])
->leftJoin('ratings', 'ratings.consultant_id', 'consultants.id')
->groupBy('consultants.id')
->orderBy('avg_rating', 'DESC')
->get();
}
答
阅读它要试试这个:
public function searchConsultants()
{
$location = $request->get('location');
$fromDate = $request->get('from_date');
$toDate = $request->get('to_date');
$data = DB::table('consultants')
->selectRaw('AVG(COALESCE(ratings.rating_for_manager, 0))as avg_rating, consultants.id,consultants.cunsultant_name,contact_number,location')
->whereBetween('date',[$fromDate,$toDate])
->orWhere('location','LIKE','%'.$location.'%')
->leftJoin('ratings', 'ratings.consultant_id', 'consultants.id')
->groupBy('consultants.id')
->orderBy('avg_rating', 'DESC')
->get();
}
希望这对你的工作!
https://*.com/a/35926066/5808894 – linktoahref