Home > other >  Class 'App\User' not found and error is being thrown
Class 'App\User' not found and error is being thrown

Time:06-22

Apologies in advance if I've missed any posting etiquette. Also to note I'm a novice PHP developer.

I'm working on an Instagram clone project from freecodecamp.org on youtube. It's titled: 'Laravel PHP Framework Tutorial - Full Course for Beginners (2019)'. I'm working through sections 1:08:30 - 1:09:30 (video timestamps). I've had everything working up until the profilecontroller was modified to display the username dynamically on the profile page.

Here are the relevant code snippets that I've been working on and that are being referenced in the traceback:

Error: Error Class 'App\User' not found http://localhost:8000/profile/1 App\Http\Controllers\ProfilesController::index C:\Users\Blake\freeCodeGram\app\Http\Controllers\ProfilesController.php:13`

Controllers/ProfileController.php

<?php



    namespace App\Http\Controllers;

    use Illuminate\Http\Request;


    class ProfilesController extends Controller

    {

public function index($user)

{



    \App\User::find($user);



    return view('home',[

        'user' => $user,

    ]);

}

}   

Web.php

<?php

    use Illuminate\Support\Facades\Route;

    Route::get('/', function () {
        return view('welcome');
    });

    Auth::routes();

    Route::get('/profile/{user}', [App\Http\Controllers\ProfilesController::class, 'index'])- 
   >name('profile.show');

Models/User.php

   namespace App\Models;

    use Illuminate\Contracts\Auth\MustVerifyEmail;
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use Illuminate\Notifications\Notifiable;
    use Laravel\Sanctum\HasApiTokens;

    class User extends Authenticatable
    {
        use HasApiTokens, HasFactory, Notifiable;

        /**
         * The attributes that are mass assignable.
         *
         * @var array<int, string>
         */
        protected $fillable = [
            'name',
            'email',
            'username',
            'password',
        ];

        /**
         * The attributes that should be hidden for serialization.
         *
         * @var array<int, string>
         */
        protected $hidden = [
            'password',
            'remember_token',
        ];

        /**
         * The attributes that should be cast.
         *
         * @var array<string, string>
         */
        protected $casts = [
            'email_verified_at' => 'datetime',
        ];
    }

Thank you for your help!

CodePudding user response:

In your controller, change the user model from \App\User to App\Models\User.

Then make sure the value of $user passed in \App\Models\User::find($user) should be an integer or array of integers.

  • Related