我如何纠正这个错误,我有三个表的profils,compes和competences_profil。 我想恢复每个profil的能力,我可以通过查询构建器和具有说服力的能力来检索profils。但是当我想要检索每个profil的权限时,我得到以下错误:未定义的属性:$ stdClass ::
controller :
$competences = \App\Competence::All();
$profils = \DB::table('profils')
->select(['profils.*']);
if($request->has('varcate')){
$profils->join('fonctions','profils.fonctions_id','=','fonctions.id');
$profils->join('categos','fonctions.categos_id','=','categos.id');
$profils->where('categos.id','=',$request->input('varcate'));
}
if($request->has('varfonction')){
if(!$request->has('varcate')){
$profils->join('fonctions','profils.fonctions_id','=','fonctions.id');
}
$profils->where('fonctions.id','=',$request->input('varfonction'));
}
if($request->has('varstate')){
$profils->join('citys','profils.citys_id','=','citys.id');
$profils->join('states','citys.state_id','=','states.id');
$profils->where('states.id','=',$request->input('varstate'));
}
if($request->has('varcity')){
if(!$request->has('varstate')){
$profils->join('citys','profils.citys_id','=','citys.id');
}
$profils->where('citys.id','=',$request->input('varcity'));
}
$profils = $profils->paginate(30);
型号: 权限 公共职能概况() { return $ this-> belongsToMany('App \ Profils'); }
profils
public function competences()
{
return $this->belongsToMany('App\Competence');
}
view :
@foreach($profils as $profil)
@foreach($profil->competences as $competence)
{{$competence->titre}}
@endforeach
@endforeach
答案 0 :(得分:0)
结帐relationship docs。您可以使用->with('competences')
描述与模型内部函数的关系,而不是在控制器内部检索它们。然后你在$ profils对象中拥有它们(关系)。
似乎您使用hasManyThrough关系,因此在Profil模型中:
/**
* Get all of the competences for profile.
*/
public function competences()
{
return $this->hasManyThrough('App\Competence', 'App\Competences_profil');
}
答案 1 :(得分:0)
试试这个
控制器:
use App/Profils
public function getProfiles(Profils $profils){
$profils = $profils;
if($request->has('varcate')){
$varcate = $request->get('varcate');
$profils = $profils->whereHas('fonctions', function ($query) use ($varcate){
$query->whereHas('category', function ($subQuery) use ($varcate) {
$subQuery->where('category.id',$varcate);
});
});
}
if($request->has('varfonction')){
$varfonction = $request->get('varfonction');
$profils = $profils->whereHas('fonctions', function ($query) use ($varfonction){
$query->where('fonctions.id',$varfonction);
});
}
if($request->has('varstate')){
$varstate = $request->get('varstate');
$profils = $profils->whereHas('citys', function ($query) use ($varstate){
$query->whereHas('states', function ($subQuery) use ($varstate) {
$subQuery->where('states.id',$varstate);
});
});
}
if($request->has('varcity')){
$varcity = $request->get('varcity');
$profils = $profils->whereHas('citys', function ($query) use ($varcity){
$query->where('citys.id',$varcity);
});
}
$profils = $profils->paginate(30);
return view('profiles',compact('profils'))
}
异型材
public function fonctions(){
return $this->belongsTo('App\fonctions','fonctions_id');
}
public function citys(){
return $this->belongsTo('App\citys','citys_id');
}
public function competences()
{
return $this->belongsToMany('App\Competence');
}
fonction
public function fonction(){
return $this->belongsTo('App\category','category_id');
}
市内
public function states(){
return $this->belongsTo('App\states','state_id');
}
观点:
@foreach($profils as $profil)
@foreach($profil->competences as $competence)
{{$competence->titre}}
@endforeach
@endforeach