我如何纠正此错误,我有三个表配置文件,权限和功能表。
我想恢复每个配置文件的能力,我可以使用查询生成器来检索配置文件,并且可以雄辩地检索性能。但是当我想恢复每个配置文件的功能时,出现以下错误:未定义的属性:$ 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);


型号
    权限
            公共函数profils()
        {
            返回$ 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

最佳答案

尝试这个

控制器:

    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');
}


功能

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

关于php - laravel未定义的属性:stdClass::$,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39085957/

10-11 10:22