我要执行一个复杂查询,取决于谁,我向大家解释:
这是我的表结构:
licencies :
-id
-lb_name
-type_licence_id
-valid_licence_id
licencies_medias :
-id
-licencie_id
-file path
我实际上进行了查询,该查询返回了具有2个或更多文件(licencies_medias)的许可集合
$licencies = $query->whereIn('type_licence_id' , [1 , 2 , 3 , 4 , 5])
->whereIn('statut_licence_id', ['1' , '4'])
->where('valid_licence_id', '1')
->has('medias', '>=', 2)
->orderBy('lb_name' , 'asc')
->paginate(10);
我的问题是媒体的数量取决于type_licence_id :
例如,一个type_licence_id = 1的许可证需要拥有3个licencies_medias(文件)
具有type_licence_id = 2的许可证需要具有4个licencies_medias(文件)
我需要创建一个查询,该查询可以检查此条件并显示所有 拥有正确数量文件的许可证 type_licence_id并一起显示
实际上在我的模型中,我有:
// on licencies model
public function medias()
{
return $this->hasMany(LicenciesMedias::class , 'licencie_id');
}
// on licenciesMedia model
public function licencie()
{
return $this->belongsTo(Licencies::class , 'licencie_id);
}
编辑它似乎可以与合并收集方法一起使用,只有一个问题是当我尝试过滤查询时却没有结果:这里是完整的控制器:
public function licenceToValidFromFede(Request $request)
{
// $licencies_to_search = Licencies::select('structure_id', 'num_licence', 'lb_nom' , 'lb_prenom' , 'id')
// ->whereIn('type_licence_id' , [1 , 2 , 3 , 4 , 5])
// ->whereIn('statut_licence_id', ['1' , '4'])
// ->where('valid_licence_id', '1')
// ->get()
// ->mapWithKeys(function($i) {
// return [$i->id => $i->num_licence.' - '.$i->lb_nom. ' ' .$i->lb_prenom. ' - ' .$i->structure->num_structure. ' ' .$i->structure->nom_structure];
// });
$type_licence = Type_licence::pluck('lb_type' , 'id');
$activite = ActiviteLicencie::pluck('lb_activite' , 'id');
$structure = Structure::select('num_structure', 'nom_structure' , 'id')
->get()
->mapWithKeys(function($i) {
return [$i->id => $i->num_structure.' - '.$i->nom_structure];
});
$catg_licence = CatgLicence::pluck('lb_catg_lic' , 'id');
$query = Licencies::query();
$filters = [
'structure' => 'structure_id',
'type_licence' => 'type_licence_id',
'activite_licencie' => 'activite_licencie_id',
'assurance' => 'lb_assurance_etat',
'catg_licence' => 'catg_licence_id',
];
foreach ($filters as $key => $column) {
if ($request->has($key)) {
$query->where($column, $request->{$key});
}
}
// $licencies = $query->whereIn('type_licence_id' , [1 , 2 , 3 , 4 , 5])
// ->whereIn('statut_licence_id', ['1' , '4'])
// ->where('valid_licence_id', '1')
// ->has('medias', '=', 2)
// ->orderBy('lb_nom' , 'asc')
// ->paginate(10);
$licencies_id_1 = $query->where('type_licence_id' , 1)
->whereIn('statut_licence_id', ['1' , '4'])
->where('valid_licence_id', '1')
->has('medias', '>=', 2)
->get();
$licencies_id_2 = $query->where('type_licence_id' , 2)
->whereIn('statut_licence_id', ['1' , '4'])
->where('valid_licence_id', '1')
->has('medias', '>=', 3)
->get();
$licencies_id_3 = $query->where('type_licence_id' , 3)
->whereIn('statut_licence_id', ['1' , '4'])
->where('valid_licence_id', '1')
->has('medias', '>=', 3)
->get();
$licencies_id_4 = $query->where('type_licence_id' , 4)
->whereIn('statut_licence_id', ['1' , '4'])
->where('valid_licence_id', '1')
->has('medias', '>=', 3)
->get();
$licencies_id_5 = $query->where('type_licence_id' , 5)
->whereIn('statut_licence_id', ['1' , '4'])
->where('valid_licence_id', '1')
->has('medias', '>=', 3)
->get();
$all_licencies = $licencies_id_1->merge($licencies_id_2);
$all_licencies = $all_licencies->merge($licencies_id_3);
$all_licencies = $all_licencies->merge($licencies_id_4);
$all_licencies = $all_licencies->merge($licencies_id_5);
$licencies = $all_licencies;
$perPage = 15;
$paginator = new Paginator($licencies, $perPage);
return view('licencie/validerFromFede' , compact('licencies' ,'licencies_to_search' , 'type_licence' , 'activite' , 'structure' , 'catg_licence' ,'paginator'));
}
答案 0 :(得分:1)
您可以通过更多查询来做到这一点。
Foreach版本
$filters = [
'structure' => ['structure_id', 1, 3]
'type_licence' => ['type_licence_id',2 ,4]
];
$all_licencies = collect();
foreach ($filters as $key => $column) {
if ($request->has($key)) {
$licencies = $query->where($column[0], $request->{$key});
->where('statut_licence_id', $column[1])
->whereIn('statut_licence_id', ['1' , '4'])
->where('valid_licence_id', '1')
->has('medias', '=', $column[2])
->get();
$all_licencies = $all_licencies->merge($licencies);
}
}
硬编码版本
$licencies_id_1 = $query->where('type_licence_id' , 1)
->whereIn('statut_licence_id', ['1' , '4'])
->where('valid_licence_id', '1')
->has('medias', 3)
->get();
$licencies_id_2 = $query->where('type_licence_id' , 2)
->whereIn('statut_licence_id', ['1' , '4'])
->where('valid_licence_id', '1')
->has('medias', 4)
->get();
然后您可以将它们合并在一起。
$all_licencies = $licencies_id_1->merge($licencies_id_2);
$all_licencies = $all_licencies->merge($licencies_id_3);
,依此类推。并在最后订购。
$ordered_licencies = $all_licencies->sortBy('lb_name');
答案 1 :(得分:0)
//You can get all licenses and getting their medias using laravel with relation
$licenses = License::whereIn('type_licence_id' , [1 , 2 , 3 , 4 , 5])
->where('valid_licence_id', '1')
->with('medias')
->get();
// This will get license medias based on the relation defined in the License model.
// You can get an idea from this, as you should not request individually for each type of 'type_license_id'.