title: Trying to get property of non-object foreach blade in laravel 5.7
body: <p>I am trying to access my course information from a foreach, but it shows this error: Trying to get property of non-object.</p>
  
  <p>I tried to access the information as follows: <code>$purchase-&gt;course-&gt;name</code>
  also like this:<br>
  <code>$ purchase[course][name]</code> but does not show any results, I have attached my model, my controller and my view.</p>
  
  <p>Model:</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Purchase extends Model
  {   
      protected $fillable = [
              'transaction_id',
              'user_id',
              'course_id',
              'status',
              'total'
      ];
  
      public function courses()
      {
          return $this-&gt;belongsTo(Course::class,'course_id');
      }
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use App\Purchase;
  
  class PurchaseController extends Controller
  {
      public function index()
      {  
          $purchases = Purchase::with('courses')-&gt;get();
          return view('purchases.index',compact('purchases'));
      }
  }
  </code></pre>
  
  <p>View:</p>
  
  <pre class="lang-php prettyprint-override"><code>@foreach($purchases as $purchase )
   {{$purchase-&gt;course-&gt;name}}
  @endforeach
  </code></pre>
  
  <p>if show only the variable like this <code>$purchase</code> all the data appears but when trying to access the course <code>$purchase-&gt;course</code> it shows nothing. 
  In the controller it works fine only not works in blade view.</p>