title: Trying to get property 'name' of non-object (View: (...) resources/views/products/index.blade.php)
body: <p>In table in view with data from products table I am traying to show names from user table but getting this error. I made Laravel relations in models and foreign keys. And added directory in product controller for user model.</p>
  <p>Error: Trying to get property 'name' of non-object (View: /home/laravel/web/laravel.swt101.eu/public_html/abonamenty/resources/views/products/index.blade.php)</p>
  <p>This is part of my controller for showing product data:</p>
  <pre><code>  public function index()
      {
          $user = User::all('name','id');        
          $products = Product::sortable()-&gt;paginate(5);
          return view('products.index',compact('products', 'user'))
              -&gt;with('i', (request()-&gt;input('page', 1) - 1) * 5);
      }
  </code></pre>
  <p>This is part of view where I am getting this error</p>
  <pre><code> &lt;td&gt;{{ $product-&gt;user-&gt;name }}&lt;/td&gt;
  </code></pre>
  <p>This is rest code of this view:</p>
  <pre><code>@extends('layouts.app')
  
  
  @section('content')
      &lt;div class=&quot;row&quot;&gt;
          &lt;div class=&quot;col-lg-12 margin-tb&quot;&gt;
              &lt;div class=&quot;pull-left&quot;&gt;
                  &lt;h2&gt;Zarządzanie abonamentami&lt;/h2&gt;
              &lt;/div&gt;
              
  &lt;div class=&quot;col-md-4&quot;&gt;
  &lt;form action=&quot;/search2&quot; method=&quot;get&quot;&gt;
  &lt;div class=&quot;input-group&quot;&gt;
  &lt;input type=&quot;search&quot; name=&quot;search&quot; class=&quot;form-control&quot;&gt;
  &lt;span class=&quot;input-group-prepend&quot;&gt;
  &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Wyszukaj&lt;/button&gt;
  &lt;/span&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  &lt;/div&gt;
              &lt;div class=&quot;pull-right&quot;&gt;
                  @can('product-create')
                  &lt;a class=&quot;btn btn-success&quot; href=&quot;{{ route('products.create') }}&quot;&gt; Utwórz nowy abonament&lt;/a&gt;
                  @endcan
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
  
      @if ($message = Session::get('success'))
          &lt;div class=&quot;alert alert-success&quot;&gt;
              &lt;p&gt;{{ $message }}&lt;/p&gt;
          &lt;/div&gt;
      @endif
  
  
      &lt;table class=&quot;table table-bordered&quot;&gt;
          &lt;tr&gt;
  
              &lt;th scope=&quot;col&quot;&gt;@sortablelink('id', 'Numer')&lt;/th&gt;
              &lt;th scope=&quot;col&quot;&gt;@sortablelink('name', 'Nazwa usługi')&lt;/th&gt;
              &lt;th scope=&quot;col&quot;&gt;@sortablelink('user_id', 'Kontrachent')&lt;/th&gt;
              &lt;th scope=&quot;col&quot;&gt;@sortablelink('category', 'Kategoria')&lt;/th&gt;
              &lt;th scope=&quot;col&quot;&gt;@sortablelink('launchdate', 'Data uruchomienia')&lt;/th&gt;
              &lt;th scope=&quot;col&quot;&gt;@sortablelink('expirationdate', 'Data wygasnięcia')&lt;/th&gt;
              &lt;th scope=&quot;col&quot;&gt;@sortablelink('renewalprice', 'Cena odnowienia')&lt;/th&gt;
              &lt;th width=&quot;280px&quot;&gt;Akcja&lt;/th&gt;
          &lt;/tr&gt;
          @foreach ($products as $product)
          &lt;tr&gt;
              &lt;td&gt;{{ ++$i }}&lt;/td&gt;
              &lt;td&gt;{{ $product-&gt;name }}&lt;/td&gt;
              &lt;td&gt;{{ $product-&gt;user-&gt;name }}&lt;/td&gt;
              &lt;td&gt;{{ $product-&gt;category }}&lt;/td&gt;
              &lt;td&gt;{{ $product-&gt;launchdate }}&lt;/td&gt;
              &lt;td&gt;{{ $product-&gt;expirationdate }}&lt;/td&gt;
              &lt;td&gt;{{ $product-&gt;renewalprice }}&lt;/td&gt;
              &lt;td&gt;
                  &lt;form action=&quot;{{ route('products.destroy',$product-&gt;id) }}&quot; method=&quot;POST&quot;&gt;
                      &lt;a class=&quot;btn btn-info&quot; href=&quot;{{ route('products.show',$product-&gt;id) }}&quot;&gt;Więcej&lt;/a&gt;
                      @can('product-edit')
                      &lt;a class=&quot;btn btn-primary&quot; href=&quot;{{ route('products.edit',$product-&gt;id) }}&quot;&gt;Edytu&lt;/a&gt;
                      @endcan
  
  
                      @csrf
                      @method('DELETE')
                      @can('product-delete')
                      &lt;button type=&quot;submit&quot; class=&quot;btn btn-danger&quot;&gt;Usuń&lt;/button&gt;
                      @endcan
                  &lt;/form&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          @endforeach
      &lt;/table&gt;
  
  {!! $products ?? ''-&gt;appends(request()-&gt;except('page'))-&gt;render() !!}
   
  
  
  
  @endsection
  </code></pre>
  <p>This is my product model:</p>
  <pre><code>    &lt;?php
  
  
  namespace App;
  
  use Kyslik\ColumnSortable\Sortable;
  use Illuminate\Database\Eloquent\Model;
  
  
  class Product extends Model
  {
      /**
       * The attributes that are mass assignable.
       *  
       * @var array
       */
      use Sortable;
      
      
      protected $fillable = [
          'name', 'detail', 'id', 'created_at', 'updated at', 'category', 'launchdate', 'expirationdate', 'renewalprice', 'user_id', 'billingperiod', 'internalcost', 'status'
      ];
      
      public $sortable = ['id', 'name', 'created_at', 'updated at', 'category', 'launchdate', 'expirationdate', 'renewalprice', 'category', 'user_id'];
      
      public function user()
      {
        return $this-&gt;belongsTo('App\User','user_id');
      }
              
      
  }
  </code></pre>
  <p>This is my user model:</p>
  <pre><code>&lt;?php
    
  namespace App;
    
  use Illuminate\Notifications\Notifiable;
  use Illuminate\Contracts\Auth\MustVerifyEmail;
  use Illuminate\Foundation\Auth\User as Authenticatable;
  use Spatie\Permission\Traits\HasRoles;
  use Kyslik\ColumnSortable\Sortable;
  use Illuminate\Database\Eloquent\Model;
  
    
  class User extends Authenticatable
  {
      use Notifiable;
      use HasRoles;
      use Sortable;
      /**
       * The attributes that are mass assignable.
       *
       * @var array
       */
      protected $fillable = [
          'name', 'email', 'password', 'surname', 'showname', 'business', 'NIP', 'PESEL', 'address', 'city', 'postalcode', 'phone', 'comments', 
      ];
      public $primaryKey = 'id';
      /**
       * The attributes that should be hidden for arrays.
       *
       * @var array
       */
      protected $hidden = [
          'password', 'remember_token',
      ];
    
      /**
       * The attributes that should be cast to native types.
       *
       * @var array
       */
      protected $casts = [
          'email_verified_at' =&gt; 'datetime',
      ];
      
      public $sortable = ['name',
                          'email',
                           'surname', 
                          'showname', 
                          'business',
                          'address',
                          'city',
                          'phone',
                          'role',
                         ];
      
          public function products()
      {
          return $this-&gt;hasMany('App\Product');
      }
          public function invoices()
      {
          return $this-&gt;hasMany('App\Invoice');
      }
  }
  </code></pre>