本文介绍了雄辩查询中的按条件分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试在查询中创建分组依据条件时出现错误,我得到此异常:
I have an error when i try to make a Group By condition in my query , i get this exception :
有人知道为什么我会收到此错误?这是因为我没有在模型中创建范围吗?在此先感谢很多朋友.
Someone knows why i get this error ? It's because i didn't created a Scope in my Model ? Thanks a lot in advance friends .
这里是我的控制器:
public function index(Request $request){
$compet = Compet::pluck('lb_compet' , 'id');
$structure = Structure::select('num_structure', 'nom_structure' , 'id')
->where('type_structure_id' , '1')
->orWhere('type_structure_id' , '2')
->orWhere('type_structure_id' , '3')
->get()
->mapWithKeys(function($i) {
return [$i->id => $i->num_structure.' - '.$i->nom_structure];
});
$catg_compet = CategorieCompet::pluck('lb_categorie_compet' , 'id');
$fonction = FonctionOfficiel::pluck('lb_fonction' , 'id');
$bareme = Bareme::pluck('lb_bareme' , 'id');
$licence = Licencies::select('lb_nom', 'num_licence', 'lb_prenom', 'id' , 'structure_id' , 'activite_licencie_id')
->where('type_licence_id' , '1')
->get()
->mapWithKeys(function($i) {
return [$i->id => $i->lb_nom.' - '.$i->lb_prenom.' - n°'.$i->num_licence.' - '.$i->activite_licencie->lb_activite.' - '.$i->structure->nom_structure];
});
$query = RencontreOfficiel::query()->orderBy('licencie_id');
$filters = [
'licencie_id' => 'licencie_id',
'compet_id' => 'compet_id',
'structure_id' => 'structure_id',
'catg_compet_id' => 'dt_rencontre',
'fonction_id' => 'dt_rencontre',
'bareme_id' => 'bareme_id',
'dt_min_rencontre' => 'dt_rencontre',
'dt_max_rencontre' => 'dt_rencontre',
];
$dt_min = $request->input('dt_rencontre_min');
$dt_max = $request->input('dt_rencontre_max');
foreach ($filters as $key => $column) {
$query->when($request->{$key}, function ($query, $value) use ($column , $dt_min , $dt_max) {
$query->where($column, $value)
->orWhereBetween('dt_rencontre' , [$dt_min , $dt_max]);
});
}
// group by licence
$designations = $query->groupBy('licencie_id');
return view('designations/index' , compact('licence' , 'designations' , 'compet' , 'structure' , 'catg_compet' , 'fonction' , 'bareme'));
}
这是我的模特:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
use Laravel\Scout\Searchable;
class Licencies extends Model
{
protected $table = 'licencies';
public $timestamps = true;
protected $fillable = array('lb_nom', 'lb_photo', 'lb_prenom', 'dt_naissance', 'cd_dept_naissance', 'lb_ville_naissance', 'adresse_email');
// use Searchable;
public function structure()
{
return $this->belongsTo('App\Structure');
}
public function club()
{
return $this->belongsTo('App\Club');
}
public function pays()
{
return $this->belongsTo('App\Pays' , 'pays_naissance_id');
}
public function activite_licencie()
{
return $this->belongsTo('App\ActiviteLicencie' , 'activite_licencie_id');
}
public function saison()
{
return $this->belongsTo('App\Saison' , 'saison_id');
}
public function statut_licence()
{
return $this->belongsTo('App\LicenceStatut');
}
public function nationalite()
{
return $this->belongsTo('App\Nationalite');
}
public function civilite()
{
return $this->belongsTo('App\Civilite');
}
public function categorie_age() {
return $this->belongsTo('App\CatgLicence' , 'catg_licence_id');
}
public function valide(){
return $this->belongsTo('App\LicenceValid' , 'valid_licence_id');
}
public function sanctions(){
return $this->hasMany('App\LicenceSanction' , 'licencie_id');
}
public function type_licence(){
return $this->belongsTo('App\Type_licence');
}
public function equipes(){
return $this->belongsToMany('App\Equipe');
}
public function selections(){
return $this->belongsToMany('App\SelectionLicence' , 'licencie_id');
}
public function getAgeAttribute()
{
return Carbon::parse($this->attributes['dt_naissance'])->diff(Carbon::now())->format('%y ans');
}
public function getSexeAttribute(){
return str_limit($this->civilite->lb_civilite, 1 , '');
}
public function getActiviteAttribute(){
return $this->activite_licencie->lb_activite;
}
//public function getNumLicencieAttribute() {
// $first = substr($this->num_licence, 0, 2); //from position 0, take 2 digits
// $second = substr($this->num_licence, 2, 2); //from position 2, take 2
// $third = substr($this->num_licence, 4); //from 4 take the rest.
// return $first . '-' . $second . '-' . $third;
// }
}
推荐答案
我认为您只是这里的Missiong模型类对象
I think you are just missiong model class object here
// group by licence
$designations = $query->groupBy('licencie_id');
您需要使用
// group by licence
$designations = Licencies::groupBy('licencie_id')->get();
这篇关于雄辩查询中的按条件分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!