Wednesday, March 15, 2017

Laravel 5.2 Trying to get property of non-object when getting data from database

i m working with laravel 5.2 i have 2 Models

Prof :

  class Prof extends Model
{
  protected $fillable=array('nom','prenom','age','mail');

public function matieres(){

    return $this->hasMany(Matiere::class,'id_prof');
}
}

Matiere :

class Matiere extends Model
{

    protected $fillable=array('Nom');

public function profs(){
        return $this->BelongsTo(Prof::class,'id_prof');
    }
}

in MatiereController i have the methode that gives me all Matieres:

public function index()
    {
        $matiere = Matiere::all();
        return view('Matiere.index',compact('matiere'));
    }

in my view i show the list of my Matiere and the name of the prof that teach this subject (Matiere) using

 @foreach ($matiere as $mat)
      @foreach ($mat->profs as $pr)
       
      @endforeach                
   @endforeach

But i get this error

Trying to get property of non-object

How could i fix this ? thnks



via Yakhlaf Yassir

Advertisement