Laravel“找不到类“\App\User””错误

问题描述 投票:0回答:1

我遇到错误:

Class "\App\User" not found

这个错误出现在我的 Laravel 应用程序中,该应用程序运行良好近一年了。它仍在我同事的笔记本电脑上运行,但我现在遇到了错误。我没有对代码进行任何更改。

auth.php:

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
],

用户.php:

<?php

namespace App\Models;

use Carbon\Carbon;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Model
{
    use SoftDeletes;

    const SUPER_ADMIN_STORE = 1;
    const COMPANY_ADMIN = 2;
    const COMPANY_STORE_USER = 3;
    const COMPANY_PROFILE_USER = 4;
    const STORE_USER = 5;

    protected $table = 'users';
    protected $hidden = ['id', 'password', 'role_id'];
    protected $fillable = [
        'slack', 'user_code', 'fullname', 'email', 'password', 'init_password', 'phone',
        'profile_image', 'role_id', 'status', 'is_admin', 'user_type', 'created_by', 'updated_by',
        'company_id', 'role_type', 'deleted_at', 'job_title'
    ];

    public function scopeActive($query)
    {
        return $query->where('users.status', 1);
    }

    public function scopeRoleJoin($query)
    {
        return $query->leftJoin('roles', function ($join) {
            $join->on('roles.id', '=', 'user_stores.role_id');
        });
    }

    public function scopeStatusJoin($query)
    {
        return $query->leftJoin('master_status', function ($join) {
            $join->on('master_status.value', '=', 'users.status');
            $join->where('master_status.key', '=', 'USER_STATUS');
        });
    }

    public function scopeSuperAdminStoreData($query)
    {
        return $query->leftJoin('stores', function ($join) {
            $join->on('stores.id', '=', 'user_stores.store_id');
        });
    }

    public function scopeClientAdminUsers($query, $logged_user_store_id)
    {
        if ($logged_user_store_id === 1) {
            return $query->where('users.is_admin', true);
        }

        return $query;
    }

    public function scopeDriver($query)
    {
        return $query->where('users.user_type', 1);
    }

    public function scopeUserStoresJoin($query)
    {
        return $query->leftJoin('user_stores', function ($join) {
            $join->on('users.id', '=', 'user_stores.user_id');
            $join->whereNull('user_stores.deleted_at');
        });
    }

    public function scopeCurrentStoreUsers($query, $logged_user_store_id)
    {
        return $query->where(function ($query) use ($logged_user_store_id) {
            $query->where('user_stores.store_id', $logged_user_store_id)
                  ->where('users.is_admin', false);

            if ($logged_user_store_id === 1) {
                $query->orWhere('users.is_admin', true);
            }
        });
    }

    public function scopeUserStoreData($query)
    {
        return $query->leftJoin('user_stores', function ($join) {
            $join->on('user_stores.store_id', '=', 'users.store_id');
            $join->where('user_stores.user_id', '=', 'users.id');
        });
    }

    public function scopeStoreData($query)
    {
        return $query->leftJoin('stores', function ($join) {
            $join->on('stores.id', '=', 'user_stores.store_id');
        });
    }

    public function scopeCompanyStoresData($query)
    {
        return $query->rightJoin('stores', function ($join) {
            $join->on('stores.company_id', '=', 'users.company_id');
        });
    }

    public function scopeHideSuperAdminRole($query)
    {
        return $query->where('user_stores.role_id', '!=', 1);
    }

    public function scopeHideCurrentLoggedUser($query, $logged_user_id)
    {
        return $query->where('users.id', '!=', $logged_user_id);
    }

    public function scopeCreatedUser($query)
    {
        return $query->leftJoin('users AS user_created', function ($join) {
            $join->on('user_created.id', '=', 'users.created_by');
        });
    }

    public function scopeUpdatedUser($query)
    {
        return $query->leftJoin('users AS user_updated', function ($join) {
            $join->on('user_created.id', '=', 'users.updated_by');
        });
    }

    public function userHasStores()
    {
        return $this->hasMany(UserStore::class, 'user_id');
    }

    public function professionalBookings($date = null)
    {
        $query = $this->hasMany(BookingService::class, 'professional_id');

        if ($date) {
            $query->whereDate('booking_date', $date);
        }

        return $query;
    }

    public function createdUser(): HasOne
    {
        return $this->hasOne('App\Models\User', 'id', 'created_by')
            ->select(['slack', 'fullname', 'email', 'user_code']);
    }

    public function updatedUser(): HasOne
    {
        return $this->hasOne('App\Models\User', 'id', 'updated_by')
            ->select(['slack', 'fullname', 'email', 'user_code']);
    }

    public function status_data(): HasOne
    {
        return $this->hasOne('App\Models\MasterStatus', 'value', 'status')->where('key', 'USER_STATUS');
    }

    public function roles($storeId): BelongsToMany
    {
        $query = $this->belongsToMany(Role::class, 'user_stores', 'user_id', 'role_id');

        if ($this->is_admin !== 1) {
            $query->whereNull('user_stores.deleted_at')
                  ->where('user_stores.store_id', '=', $storeId);
        }

        return $query;
    }

    public function stores(): BelongsToMany
    {
        return $this->belongsToMany('App\Models\Store', 'user_stores')
            ->withPivot(['created_by', 'created_at', 'updated_at']);
    }

    public function storesIds(): array
    {
        return $this->belongsToMany('App\Models\Store', 'user_stores')
            ->pluck('store_id')->toArray();
    }

    public function parseDate($date): ?string
    {
        return ($date != null) ? Carbon::parse($date)->format(config("app.date_time_format")) : null;
    }

    public function services()
    {
        return $this->belongsToMany(Service::class, 'user_services')->withoutGlobalScopes();
    }

    public function serializeDate($date): ?string
    {
        return ($date != null) ? $date->format('Y-m-d H:i:s') : null;
    }

    public function company(): ?BelongsTo
    {
        return $this->belongsTo(Company::class);
    }
}

采取的故障排除步骤:

  1. 编辑了
    auth.php
    中的配置以匹配
    User.php
    中的命名空间声明,即使它之前工作正常。
  2. 运行
    composer dump-autoload
    并清除所有缓存。

系统信息:

  • PHP版本:
    8.2.0
  • 作曲版本:
    2.8.3
  • Laravel 版本:
    8.83.29
  • 操作系统:Windows 10
php laravel error-handling composer-php
1个回答
0
投票

问题是您的用户模型位于 App/Model 命名空间下

所以你必须更新 auth.php 文件如下

'providers' => [
'users' => [
    'driver' => 'eloquent',
    'model' => App\Model\User::class,
],

],

它会工作得很好

© www.soinside.com 2019 - 2024. All rights reserved.