代码之家  ›  专栏  ›  技术社区  ›  Mathieu Mourareau

具有条件的查询集合雄辩的Laravel

  •  0
  • Mathieu Mourareau  · 技术社区  · 6 年前

    我有一个复杂的查询要做谁取决于我的参数,我向大家解释:

    以下是我的表格结构:

    licencies : 
    -id 
    -lb_name
    -type_licence_id
    -valid_licence_id 
    
    
    
    licencies_medias : 
    -id
    -licencie_id
    -file path
    

    我实际上做了一个查询,谁返回了一个具有2个或更多文件的许可证集合(licensions_media)

     $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);
    

    我的问题是媒体的数量取决于类型\许可证\ 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'));
        }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   James    6 年前

    你可以通过更多的查询来完成。

            $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');
    
        2
  •  0
  •   Zeeshan Ahmed    6 年前
    //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'.