laravel5.4过滤器

时间:2017-08-24 05:43:57

标签: laravel laravel-5 laravel-5.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();
}

通过上述查询,我​​可以获取数据,但有时我不想搜索日期我只想搜索位置,

上述查询中的问题我必须输入位置和日期来过滤用户,那么如何只使用位置或日期进行过滤。

2 个答案:

答案 0 :(得分:0)

使用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();
}

答案 1 :(得分:0)

你应该试试这个:

constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, private alertCtrl: AlertController, private network: Network, public deeplinks: Deeplinks, public events: Events) {
 platform.ready().then(() => {
  let disconnectSub = this.network.onDisconnect().subscribe(() => {
    console.log('you are offline');
  });

 let connectSub = this.network.onConnect().subscribe(()=> {
    console.log('you are online');
 });

 if(this.network.type === 'none' || this.network.type === 'unknown') {
  let alert = this.alertCtrl.create({
    title: "Internet Connection Problem",
    subTitle:"Please Check Your Network connection",
    buttons: [{
      text: 'Ok',
      handler: () => {
        platform.exitApp();
      }
    }]
  });
  alert.present();
}

});
}