title: Laravel 5.6 - Class name must be a valid object or a string
body: <p>My Laravel application is throwing the next error:</p>
  
  <pre><code>(1/1) Error
  
  Class name must be a valid object or a string
  in HasRelationships.php line 656
  </code></pre>
  
  <p>I have to said that right now this project is in a shared hosting but that's the only part that does not works at all. This is the HTML file:</p>
  
  <pre class="lang-php prettyprint-override"><code>@extends('layouts.app')
  
  @section('content')
  &lt;div class="container"&gt;
      &lt;div class="row justify-content-center"&gt;
          &lt;div class="col-md-10"&gt;
              &lt;div class="card"&gt;
                  &lt;div class="card-header text-uppercase text-center"&gt;&lt;b&gt;Dashboard administrativo&lt;/b&gt;&lt;/div&gt;
                  &lt;div class="card-body"&gt;
  
                    &lt;div class="card mb-3"&gt;
                      &lt;div class="card-header"&gt;
                        &lt;button type="button" class="btn btn-sm btn-primary text-uppercase" style="cursor:default;"&gt;Filtrar por&lt;/button&gt;
                        &lt;a class="btn btn-sm btn-secondary text-uppercase" role="button" href="/admin/dashboard/?gender=true"&gt;Hombre&lt;/a&gt;
                        &lt;a class="btn btn-sm btn-secondary text-uppercase" role="button" href="/admin/dashboard/?gender=false"&gt;Mujer&lt;/a&gt;
                        &lt;button type="button" class="btn btn-sm btn-primary text-uppercase" style="cursor:default;"&gt;Ordenar de manera&lt;/button&gt;
                        &lt;a class="btn btn-sm btn-secondary text-uppercase" role="button" href="{{ route('admin.dashboard', ['gender' =&gt; request('gender'), 'sort' =&gt; 'desc']) }}"&gt;Ascendente&lt;/a&gt;
                        &lt;a class="btn btn-sm btn-secondary text-uppercase" role="button" href="{{ route('admin.dashboard', ['gender' =&gt; request('gender'), 'sort' =&gt; 'asc']) }}"&gt;Descendente&lt;/a&gt;
                        &lt;a class="btn btn-sm btn-success text-uppercase" role="button" href="{{ route('admin.dashboard') }}"&gt;Reiniciar&lt;/a&gt;
                      &lt;/div&gt;
                    &lt;/div&gt;
                    &lt;div class="card"&gt;
                      &lt;div class="card-header text-uppercase text-center"&gt;Usuarios registrados en el sistema&lt;/div&gt;
                      &lt;div class="card-body"&gt;
                        &lt;table class="table table-bordered"&gt;
                          &lt;thead&gt;
                            &lt;tr&gt;
                              &lt;th class="text-center"&gt;#&lt;/th&gt;
                              &lt;th&gt;Nombre&lt;/th&gt;
                              &lt;th&gt;Correo&lt;/th&gt;
                              &lt;th&gt;Opciones&lt;/th&gt;
                            &lt;/tr&gt;
                          &lt;/thead&gt;
                          &lt;tbody&gt;
                            @foreach ($users as $user)
                              &lt;tr&gt;
                                &lt;td class="text-center"&gt;{{ $user-&gt;id }}&lt;/td&gt;
                                &lt;td&gt;{{ $user-&gt;name }}&lt;/td&gt;
                                &lt;td&gt;{{ $user-&gt;email }}&lt;/td&gt;
                                &lt;td&gt;
                                  &lt;div class="btn-group fullwidth" role="group"&gt;
                                    &lt;a href="{{ route('user.show', $user-&gt;id) }}" class="btn btn-primary" role="button"&gt;Ver&lt;/a&gt;
                                    &lt;a href="{{ route('user.edit', $user-&gt;id) }}" class="btn btn-success" role="button"&gt;Editar&lt;/a&gt;
                                  &lt;/div&gt;
                                &lt;/td&gt;
                              &lt;/tr&gt;
                            @endforeach
                          &lt;/tbody&gt;
                        &lt;/table&gt;
                        {{ $users-&gt;links() }}
                      &lt;/div&gt;
                    &lt;/div&gt;
  
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  @endsection
  </code></pre>
  
  <p>And this is my controller:</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use App\Http\Controllers\Controller;
  use Illuminate\Http\Request;
  use Auth;
  
  use App\Role;
  use App\Permission;
  use App\User;
  use App\Share;
  
  class AdminController extends Controller
  {
      public function dashboard()
      {
  
        $users = new User();
        $queries = [];
  
        $columns = [
            'gender',
        ];
  
        foreach ($columns as $column) {
  
            if(request()-&gt;has($column)) {
                $users = $users-&gt;where($column, request($column));
                $queries[$column] = request($column);
            } // if
  
        } // foreach
  
        if(request()-&gt;has('sort')) {
            $users = $users-&gt;orderBy('name', request('sort'));
            $queries['sort'] = request('sort');
        } // if
  
        $users = $users-&gt;paginate(10)-&gt;appends($queries);
  
        return view('admin.dashboard', compact('users'));
  
      } // dashboard
  }
  </code></pre>
  
  <p>Why does my code works perfectly while developing in my machine but it does not while hosted? I was looking online for a solution but everything i got was related with <code>Entrust</code> which i am not using at all. Any idea?</p>
  
  <p>Edit: <code>App\User</code></p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Notifications\Notifiable;
  use Illuminate\Foundation\Auth\User as Authenticatable;
  use Spatie\Permission\Traits\HasRoles;
  
  class User extends Authenticatable
  {
      use Notifiable, HasRoles;
  
      /**
       * The attributes that are mass assignable.
       *
       * @var array
       */
      protected $fillable = [
          'name', 'email', 'password', 'gender', 'identification', 'phone', 'country', 'city', 'provider', 'provider_id', 'birthday', 'referred_by', 'affiliate_id',
      ];
  
      /**
       * The attributes that should be hidden for arrays.
       *
       * @var array
       */
      protected $hidden = [
          'password', 'remember_token',
      ];
  
      public function shares()
      {
        return $this-&gt;hasMany('App\Share');
      }
  
      public function storeProvider()
      {
        return $this-&gt;hasOne('App\Provider');
      }
  
      public function messages()
      {
        return $this-&gt;hasMany('App\Message');
      }
  }
  </code></pre>
  
  <p>This is the whole error:</p>
  
  <pre><code> (1/1) Error
  
  Class name must be a valid object or a string
  in HasRelationships.php line 656
  at Model-&gt;newRelatedInstance(null)in HasRelationships.php line 479
  at Model-&gt;morphToMany(null, 'model', null, null, 'role_id')in HasRoles.php line 47
  at User-&gt;roles()in HasAttributes.php line 411
  at Model-&gt;getRelationshipFromMethod('roles')in HasAttributes.php line 397
  at Model-&gt;getRelationValue('roles')in HasAttributes.php line 327
  at Model-&gt;getAttribute('roles')in Model.php line 1475
  at Model-&gt;__get('roles')in HasRoles.php line 158
  at User-&gt;hasRole('administrador')in HasRoles.php line 167
  at User-&gt;hasRole(array('administrador'))in HasRoles.php line 187
  at User-&gt;hasAnyRole(array('administrador'))in RoleMiddleware.php line 21
  at RoleMiddleware-&gt;handle(object(Request), object(Closure), 'administrador')in Pipeline.php line 151
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request))in CheckInformation.php line 36
  at CheckInformation-&gt;handle(object(Request), object(Closure))in Pipeline.php line 151
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request))in SubstituteBindings.php line 41
  at SubstituteBindings-&gt;handle(object(Request), object(Closure))in Pipeline.php line 151
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request))in Authenticate.php line 43
  at Authenticate-&gt;handle(object(Request), object(Closure))in Pipeline.php line 151
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request))in VerifyCsrfToken.php line 68
  at VerifyCsrfToken-&gt;handle(object(Request), object(Closure))in Pipeline.php line 151
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request))in ShareErrorsFromSession.php line 49
  at ShareErrorsFromSession-&gt;handle(object(Request), object(Closure))in Pipeline.php line 151
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request))in StartSession.php line 63
  at StartSession-&gt;handle(object(Request), object(Closure))in Pipeline.php line 151
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request))in AddQueuedCookiesToResponse.php line 37
  at AddQueuedCookiesToResponse-&gt;handle(object(Request), object(Closure))in Pipeline.php line 151
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request))in EncryptCookies.php line 66
  at EncryptCookies-&gt;handle(object(Request), object(Closure))in Pipeline.php line 151
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request))in Pipeline.php line 104
  at Pipeline-&gt;then(object(Closure))in Router.php line 667
  at Router-&gt;runRouteWithinStack(object(Route), object(Request))in Router.php line 642
  at Router-&gt;runRoute(object(Request), object(Route))in Router.php line 608
  at Router-&gt;dispatchToRoute(object(Request))in Router.php line 597
  at Router-&gt;dispatch(object(Request))in Kernel.php line 176
  at Kernel-&gt;Illuminate\Foundation\Http\{closure}(object(Request))in Pipeline.php line 30
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request))in TrustProxies.php line 57
  at TrustProxies-&gt;handle(object(Request), object(Closure))in Pipeline.php line 151
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request))in TransformsRequest.php line 31
  at TransformsRequest-&gt;handle(object(Request), object(Closure))in Pipeline.php line 151
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request))in TransformsRequest.php line 31
  at TransformsRequest-&gt;handle(object(Request), object(Closure))in Pipeline.php line 151
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request))in ValidatePostSize.php line 27
  at ValidatePostSize-&gt;handle(object(Request), object(Closure))in Pipeline.php line 151
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request))in CheckForMaintenanceMode.php line 62
  at CheckForMaintenanceMode-&gt;handle(object(Request), object(Closure))in Pipeline.php line 151
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request))in Pipeline.php line 104
  at Pipeline-&gt;then(object(Closure))in Kernel.php line 151
  at Kernel-&gt;sendRequestThroughRouter(object(Request))in Kernel.php line 116
  at Kernel-&gt;handle(object(Request))in index.php line 55
  </code></pre>