搜索具有过滤日期的 laravel 关系
Search through laravel relationship with filtered date
i 在13-04-2022 到14-04-2022 的日期范围内搜索数据1(该数据存在于数据库中)(该日期范围内没有数据)。从我在我的控制器上使用的这个查询仍然 return 包含我搜索的关键字的数据(数据 1)。我想要的是 return 收集具有日期过滤器范围的数据(这没什么,因为我没有该日期范围内的任何数据)。
如何解决这个问题呢?在筛选日期内,我可以为此搜索实施任何最佳做法吗?
这是我的后端 :
$rekans = Rekan::query()
->join('dokters','dokters.id','=','rekans.dokter_id')
->join('pasiens','pasiens.id','=','rekans.pasien_id')
->join('penyakits','penyakits.id','=','rekans.dokter_id')
->join('treatments','treatments.id','=','rekans.treatment_id');
if(request('from_date')){
$rekans->whereDate('rekans.created_at','>=',request('from_date'));
}
if(request('to_date')){
$rekans->whereDate('rekans.created_at','<=',request('to_date'));
}
if(request('search')){
$rekans->where('berat','LIKE','%'.request('search').'%')
->orWhere('rekan_inv','LIKE','%'.request('search').'%')
->orWhere('suhu','LIKE','%'.request('search').'%')
->orWhere('hasil_pemeriksaan','LIKE','%'.request('search').'%')
->orWhere('anamnesa','LIKE','%'.request('search').'%')
->orWhere('pengobatan','LIKE','%'.request('search').'%')
->orWhere('kasus','LIKE','%'.request('search').'%')
->orWhere('dokters.nama_dokter','LIKE','%'.request('search').'%')
->orWhere('pasiens.nama','LIKE','%'.request('search').'%')
->orWhere('penyakits.nama_penyakit','LIKE','%'.request('search').'%')
->orWhere('treatments.nama_treatment','LIKE','%'.request('search').'%');
}
这是我的sql
select * from `rekans` inner join `dokters` on `dokters`.`id` = `rekans`.`dokter_id` inner join `pasiens` on `pasiens`.`id` = `rekans`.`pasien_id` inner join `penyakits` on `penyakits`.`id` = `rekans`.`dokter_id` inner join `treatments` on `treatments`.`id` = `rekans`.`treatment_id` where date(`rekans`.`created_at`) >= ? and date(`rekans`.`created_at`) <= ? and `berat` LIKE ? or `rekan_inv` LIKE ? or `suhu` LIKE ? or `hasil_pemeriksaan` LIKE ? or `anamnesa` LIKE ? or `pengobatan` LIKE ? or `kasus` LIKE ? or `dokters`.`nama_dokter` LIKE ? or `pasiens`.`nama` LIKE ? or `penyakits`.`nama_penyakit` LIKE ? or `treatments`.`nama_treatment` LIKE ? order by `rekans`.`created_at` desc
再次感谢您给我的参考,为了解决我的问题,我将搜索和日期过滤器的后端更改为此
if(request('from_date')){
$rekans->whereDate('rekans.created_at','>=',request('from_date'));
}
if(request('to_date')){
$rekans->whereDate('rekans.created_at','<=',request('to_date'));
}
if(request('search')){
$rekans->where(function ($query){
$query->where('rekans.berat','LIKE','%'.request('search').'%')
->orWhere('rekans.rekan_inv','LIKE','%'.request('search').'%')
->orWhere('rekans.suhu','LIKE','%'.request('search').'%')
->orWhere('rekans.hasil_pemeriksaan','LIKE','%'.request('search').'%')
->orWhere('rekans.anamnesa','LIKE','%'.request('search').'%')
->orWhere('rekans.pengobatan','LIKE','%'.request('search').'%')
->orWhere('rekans.kasus','LIKE','%'.request('search').'%')
->orWhere('dokters.nama_dokter','LIKE','%'.request('search').'%')
->orWhere('pasiens.nama','LIKE','%'.request('search').'%')
->orWhere('penyakits.nama_penyakit','LIKE','%'.request('search').'%')
->orWhere('treatments.nama_treatment','LIKE','%'.request('search').'%');
});
}
i 在13-04-2022 到14-04-2022 的日期范围内搜索数据1(该数据存在于数据库中)(该日期范围内没有数据)。从我在我的控制器上使用的这个查询仍然 return 包含我搜索的关键字的数据(数据 1)。我想要的是 return 收集具有日期过滤器范围的数据(这没什么,因为我没有该日期范围内的任何数据)。 如何解决这个问题呢?在筛选日期内,我可以为此搜索实施任何最佳做法吗? 这是我的后端 :
$rekans = Rekan::query()
->join('dokters','dokters.id','=','rekans.dokter_id')
->join('pasiens','pasiens.id','=','rekans.pasien_id')
->join('penyakits','penyakits.id','=','rekans.dokter_id')
->join('treatments','treatments.id','=','rekans.treatment_id');
if(request('from_date')){
$rekans->whereDate('rekans.created_at','>=',request('from_date'));
}
if(request('to_date')){
$rekans->whereDate('rekans.created_at','<=',request('to_date'));
}
if(request('search')){
$rekans->where('berat','LIKE','%'.request('search').'%')
->orWhere('rekan_inv','LIKE','%'.request('search').'%')
->orWhere('suhu','LIKE','%'.request('search').'%')
->orWhere('hasil_pemeriksaan','LIKE','%'.request('search').'%')
->orWhere('anamnesa','LIKE','%'.request('search').'%')
->orWhere('pengobatan','LIKE','%'.request('search').'%')
->orWhere('kasus','LIKE','%'.request('search').'%')
->orWhere('dokters.nama_dokter','LIKE','%'.request('search').'%')
->orWhere('pasiens.nama','LIKE','%'.request('search').'%')
->orWhere('penyakits.nama_penyakit','LIKE','%'.request('search').'%')
->orWhere('treatments.nama_treatment','LIKE','%'.request('search').'%');
}
这是我的sql
select * from `rekans` inner join `dokters` on `dokters`.`id` = `rekans`.`dokter_id` inner join `pasiens` on `pasiens`.`id` = `rekans`.`pasien_id` inner join `penyakits` on `penyakits`.`id` = `rekans`.`dokter_id` inner join `treatments` on `treatments`.`id` = `rekans`.`treatment_id` where date(`rekans`.`created_at`) >= ? and date(`rekans`.`created_at`) <= ? and `berat` LIKE ? or `rekan_inv` LIKE ? or `suhu` LIKE ? or `hasil_pemeriksaan` LIKE ? or `anamnesa` LIKE ? or `pengobatan` LIKE ? or `kasus` LIKE ? or `dokters`.`nama_dokter` LIKE ? or `pasiens`.`nama` LIKE ? or `penyakits`.`nama_penyakit` LIKE ? or `treatments`.`nama_treatment` LIKE ? order by `rekans`.`created_at` desc
再次感谢您给我的参考,为了解决我的问题,我将搜索和日期过滤器的后端更改为此
if(request('from_date')){
$rekans->whereDate('rekans.created_at','>=',request('from_date'));
}
if(request('to_date')){
$rekans->whereDate('rekans.created_at','<=',request('to_date'));
}
if(request('search')){
$rekans->where(function ($query){
$query->where('rekans.berat','LIKE','%'.request('search').'%')
->orWhere('rekans.rekan_inv','LIKE','%'.request('search').'%')
->orWhere('rekans.suhu','LIKE','%'.request('search').'%')
->orWhere('rekans.hasil_pemeriksaan','LIKE','%'.request('search').'%')
->orWhere('rekans.anamnesa','LIKE','%'.request('search').'%')
->orWhere('rekans.pengobatan','LIKE','%'.request('search').'%')
->orWhere('rekans.kasus','LIKE','%'.request('search').'%')
->orWhere('dokters.nama_dokter','LIKE','%'.request('search').'%')
->orWhere('pasiens.nama','LIKE','%'.request('search').'%')
->orWhere('penyakits.nama_penyakit','LIKE','%'.request('search').'%')
->orWhere('treatments.nama_treatment','LIKE','%'.request('search').'%');
});
}