title: Laravel 5.2 Trying to get property of non-object when getting data from database
body: <p>i m working with laravel 5.2 i have 2 Models </p>
  
  <p>Prof :</p>
  
  <pre><code>  class Prof extends Model
  {
    protected $fillable=array('nom','prenom','age','mail');
  
  public function matieres(){
  
      return $this-&gt;hasMany(Matiere::class,'id_prof');
  }
  }
  </code></pre>
  
  <p>Matiere : </p>
  
  <pre><code>class Matiere extends Model
  {
  
      protected $fillable=array('Nom');
  
  public function profs(){
          return $this-&gt;BelongsTo(Prof::class,'id_prof');
      }
  }
  </code></pre>
  
  <p>in MatiereController i have the methode that gives me all Matieres:</p>
  
  <pre><code>public function index()
      {
          $matiere = Matiere::all();
          return view('Matiere.index',compact('matiere'));
      }
  </code></pre>
  
  <p>in my view i show the list of my Matiere and the name of the prof that teach this subject (Matiere) using</p>
  
  <pre><code> @foreach ($matiere as $mat)
        @foreach ($mat-&gt;profs as $pr)
         {{$pr-&gt;prenom}}
        @endforeach                
     @endforeach
  </code></pre>
  
  <p>But i get this error</p>
  
  <p><strong>Trying to get property of non-object</strong></p>
  
  <p>How could i fix this ? thnks</p>