You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 5.5中use引入失效:已加use app\user仍报类找不到错误

Fixing "Class 'app\user' not found" in Laravel 5.5

Hey there! Let's get that frustrating class not found error sorted out. I've dealt with this exact issue a handful of times in Laravel, so here's what's going wrong and how to fix it step by step:

The Core Issues

Laravel relies heavily on PSR-4 autoloading rules and strict naming conventions—small typos or case mismatches are usually the culprit here.

1. Incorrect Namespace & Class Case

Your use app\user; statement has two critical issues:

  • Laravel's default model namespace is App (capitalized), not lowercase app
  • Model class names should follow PascalCase (capital first letter), so it's User, not user

2. Mismatched Model Filename

Make sure your model file is named User.php (capitalized) and lives directly in the app/ directory. PSR-4 requires the filename to exactly match the class name (including case), so a user.php file won't be picked up by the autoloader.

3. Bonus: Controller Naming Convention

While not the direct cause of your error, your controller class userController doesn't follow Laravel's standards. It should be UserController (PascalCase) with a matching UserController.php filename—this avoids future confusion and keeps your code consistent with framework norms.

Corrected Code Example

Here's how your controller should look after fixing these issues:

<?php

namespace App\Http\Controllers;

use App\User; // Correct namespace and class name
use Illuminate\Routing\Controller;

class UserController extends Controller // Properly capitalized controller class
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = User::all(); // Use the capitalized User class
        return view('user.index', compact('data'));
    }
}

Quick Checklist to Verify

  • Confirm app/User.php exists and contains the class User extends Model definition (with the correct top-level namespace namespace App;)
  • Double-check that your use statement uses App\User (not lowercase)
  • Ensure you're calling User::all() (capital U) in your controller method

That should resolve the "Class not found" error for you!

内容的提问来源于stack exchange,提问作者Adil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:50:41