Laravel 5.6-类名必须是有效的对象或字符串

时间:2018-08-29 23:58:50

标签: laravel

我的Laravel应用程序抛出下一个错误:

(1/1) Error

Class name must be a valid object or a string
in HasRelationships.php line 656

我必须说,目前该项目位于共享主机中,但这是唯一根本无法使用的部分。这是HTML文件:

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-10">
            <div class="card">
                <div class="card-header text-uppercase text-center"><b>Dashboard administrativo</b></div>
                <div class="card-body">

                  <div class="card mb-3">
                    <div class="card-header">
                      <button type="button" class="btn btn-sm btn-primary text-uppercase" style="cursor:default;">Filtrar por</button>
                      <a class="btn btn-sm btn-secondary text-uppercase" role="button" href="/admin/dashboard/?gender=true">Hombre</a>
                      <a class="btn btn-sm btn-secondary text-uppercase" role="button" href="/admin/dashboard/?gender=false">Mujer</a>
                      <button type="button" class="btn btn-sm btn-primary text-uppercase" style="cursor:default;">Ordenar de manera</button>
                      <a class="btn btn-sm btn-secondary text-uppercase" role="button" href="{{ route('admin.dashboard', ['gender' => request('gender'), 'sort' => 'desc']) }}">Ascendente</a>
                      <a class="btn btn-sm btn-secondary text-uppercase" role="button" href="{{ route('admin.dashboard', ['gender' => request('gender'), 'sort' => 'asc']) }}">Descendente</a>
                      <a class="btn btn-sm btn-success text-uppercase" role="button" href="{{ route('admin.dashboard') }}">Reiniciar</a>
                    </div>
                  </div>
                  <div class="card">
                    <div class="card-header text-uppercase text-center">Usuarios registrados en el sistema</div>
                    <div class="card-body">
                      <table class="table table-bordered">
                        <thead>
                          <tr>
                            <th class="text-center">#</th>
                            <th>Nombre</th>
                            <th>Correo</th>
                            <th>Opciones</th>
                          </tr>
                        </thead>
                        <tbody>
                          @foreach ($users as $user)
                            <tr>
                              <td class="text-center">{{ $user->id }}</td>
                              <td>{{ $user->name }}</td>
                              <td>{{ $user->email }}</td>
                              <td>
                                <div class="btn-group fullwidth" role="group">
                                  <a href="{{ route('user.show', $user->id) }}" class="btn btn-primary" role="button">Ver</a>
                                  <a href="{{ route('user.edit', $user->id) }}" class="btn btn-success" role="button">Editar</a>
                                </div>
                              </td>
                            </tr>
                          @endforeach
                        </tbody>
                      </table>
                      {{ $users->links() }}
                    </div>
                  </div>

                </div>
            </div>
        </div>
    </div>
</div>
@endsection

这是我的控制器:

<?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()->has($column)) {
              $users = $users->where($column, request($column));
              $queries[$column] = request($column);
          } // if

      } // foreach

      if(request()->has('sort')) {
          $users = $users->orderBy('name', request('sort'));
          $queries['sort'] = request('sort');
      } // if

      $users = $users->paginate(10)->appends($queries);

      return view('admin.dashboard', compact('users'));

    } // dashboard
}

为什么在我的机器上开发时我的代码可以完美运行,而在托管时却不能正常运行?我当时在网上寻找解决方案,但是我得到的所有内容都与Entrust相关,而我根本没有使用过。有想法吗?

编辑:App\User

<?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->hasMany('App\Share');
    }

    public function storeProvider()
    {
      return $this->hasOne('App\Provider');
    }

    public function messages()
    {
      return $this->hasMany('App\Message');
    }
}

这是整个错误:

 (1/1) Error

Class name must be a valid object or a string
in HasRelationships.php line 656
at Model->newRelatedInstance(null)in HasRelationships.php line 479
at Model->morphToMany(null, 'model', null, null, 'role_id')in HasRoles.php line 47
at User->roles()in HasAttributes.php line 411
at Model->getRelationshipFromMethod('roles')in HasAttributes.php line 397
at Model->getRelationValue('roles')in HasAttributes.php line 327
at Model->getAttribute('roles')in Model.php line 1475
at Model->__get('roles')in HasRoles.php line 158
at User->hasRole('administrador')in HasRoles.php line 167
at User->hasRole(array('administrador'))in HasRoles.php line 187
at User->hasAnyRole(array('administrador'))in RoleMiddleware.php line 21
at RoleMiddleware->handle(object(Request), object(Closure), 'administrador')in Pipeline.php line 151
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))in CheckInformation.php line 36
at CheckInformation->handle(object(Request), object(Closure))in Pipeline.php line 151
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))in SubstituteBindings.php line 41
at SubstituteBindings->handle(object(Request), object(Closure))in Pipeline.php line 151
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))in Authenticate.php line 43
at Authenticate->handle(object(Request), object(Closure))in Pipeline.php line 151
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))in VerifyCsrfToken.php line 68
at VerifyCsrfToken->handle(object(Request), object(Closure))in Pipeline.php line 151
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))in Pipeline.php line 151
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))in StartSession.php line 63
at StartSession->handle(object(Request), object(Closure))in Pipeline.php line 151
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))in Pipeline.php line 151
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))in EncryptCookies.php line 66
at EncryptCookies->handle(object(Request), object(Closure))in Pipeline.php line 151
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))in Pipeline.php line 104
at Pipeline->then(object(Closure))in Router.php line 667
at Router->runRouteWithinStack(object(Route), object(Request))in Router.php line 642
at Router->runRoute(object(Request), object(Route))in Router.php line 608
at Router->dispatchToRoute(object(Request))in Router.php line 597
at Router->dispatch(object(Request))in Kernel.php line 176
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request))in TrustProxies.php line 57
at TrustProxies->handle(object(Request), object(Closure))in Pipeline.php line 151
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))in TransformsRequest.php line 31
at TransformsRequest->handle(object(Request), object(Closure))in Pipeline.php line 151
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))in TransformsRequest.php line 31
at TransformsRequest->handle(object(Request), object(Closure))in Pipeline.php line 151
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))in ValidatePostSize.php line 27
at ValidatePostSize->handle(object(Request), object(Closure))in Pipeline.php line 151
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))in CheckForMaintenanceMode.php line 62
at CheckForMaintenanceMode->handle(object(Request), object(Closure))in Pipeline.php line 151
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))in Pipeline.php line 104
at Pipeline->then(object(Closure))in Kernel.php line 151
at Kernel->sendRequestThroughRouter(object(Request))in Kernel.php line 116
at Kernel->handle(object(Request))in index.php line 55

2 个答案:

答案 0 :(得分:0)

似乎您正在使用Spatie / Laravel权限,并且如果您的应用程序在开发服务器上运行,我想您的实时服务器中的php版本低于此软件包要求的7.0。

您应该使用phpinfo()检查工作的php版本;在文件上并将您的浏览器指向该文件,这将显示您的网络服务器使用的是哪个php版本。 例如:您可以写下具有以下内容的文件info.php

<?php
phpinfo();

希望这会有所帮助。

答案 1 :(得分:0)

如@erubiel和@ farid-silva所述,您可能需要在生产时使用PHP版本。

但是,最直接的问题是Spatie\Permission\Traits\HasRoles::roles用以下方式调用Illuminate\Database\Eloquent\Concerns\HasRelationships::morphToMany

  • related
  • table
  • foreignKeyPivot

全部设置为null

如果您查看code,这些值来自Laravel配置,即:

  • permission.models.role
  • permission.table_names.model_has_roles
  • permission.column_names.model_morph_key

因此,值得检查一下您的配置(即permission值集),以检查这些值是否在生产中正确定义。