当我尝试在查询中创建一个Group By条件时出错,我得到了这个例外:
调用未定义的方法Illuminate \ Database \ Query \ Builder :: isEmpty()
有人知道我为什么会收到这个错误?这是因为我没有在我的模型中创建一个Scope?非常感谢朋友。
这是我的控制器:
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;
// }
}
答案 0 :(得分:1)
我认为你只是这里的missiong模型类对象
// group by licence
$designations = $query->groupBy('licencie_id');
您需要使用类似
的模型的Licencies对象// group by licence
$designations = Licencies::groupBy('licencie_id')->get();