Class 'App\Model' not found after add Bican\Roles - php

I'm using the library Bican Roles. I change User.php for:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Bican\Roles\Traits\HasRoleAndPermission;
use Bican\Roles\Contracts\HasRoleAndPermission as HasRoleAndPermissionContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract, HasRoleAndPermissionContract
{
use Authenticatable, CanResetPassword, HasRoleAndPermission,Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'surnames', 'email', 'password','phone',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
When try to register a new userthrow the following error:
Class 'App \ Model' not found
I have tried to add it
use User;
etc but still not working, any ideas? Thank you

You need to add
use Illuminate\Database\Eloquent\Model;
to the top of your class declaration, not use User;
The error you're getting is
Class 'App \ Model' not found
not
Class 'User' not found

try
use Illuminate\Database\Eloquent\Model;

Related

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

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!
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.

"Add [name] to fillable property to allow mass assignment on [Illuminate\Foundation\Auth\User]."

User.php code,
here, whether I use fillable or gaurded, I get the same error.
<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
*#var array
*/
// protected $fillable = [
// 'name',
// 'email',
// 'password',
// ];
protected $guarded = [];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
UserController.php code,
here, I have tried the mass assignment
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\User;
use Illuminate\Database\Eloquent\Model;
class UserController extends Controller
{
public function index()
{
$data = [
'name' => 'elon',
'email' => 'elon#gmail.com',
'password' => 'password',
];
User::create($data);
$user = User::all();
return $user;
}
}
You seem to not be importing the user class from the right namespace in your UserController.php
You are using
use Illuminate\Foundation\Auth\User;
Use
use App\Models\User;
instead.
Edit:
$fillable is not the problem in this case as $guarded is set to an empty array which allows for all fields to be mass assignable through the create method. Eloquent mass assignment
There are two problems in the code provided:
As commented by #sta, you should allow the Model attributes to be mass assignable by using the $fillable property in the User class:
<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
*#var array
*/
protected $fillable = [
'name',
'email',
'password',
];
protected $guarded = [];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
As commented by #Remy, we should make sure to use the correct class:
<?php
namespace App\Http\Controllers;
use App\Models\User; // <-- corrected line
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
class UserController extends Controller
{
public function index()
{
$data = [
'name' => 'elon',
'email' => 'elon#gmail.com',
'password' => 'password',
];
User::create($data);
$user = User::all();
return $user;
}
}
I found this helpful for me in laravel 8, this worked fine in all versions because many times if we import class and it auto import another one so please check that you import this class or another one.
use App\Models\User;
in the UserController.php try to use
use App\Models\User;
in laravel 7 this work for me :
use App\User;
For me, I had to stop the server in terminal with ctrl + c and the restart the server with php artisan serve It worked for me.
by adding this in my model
protected $guarded = [];
it save me from my misery thanks!

PHP Error: Class 'Illuminate/Foundation/Auth/Admin' [Laravel 8]

I have issues with the php error when I tried to insert data for admin using the tinker. I'm creating a multi authentication user which is one for user and one for admin.
PHP Error: Class 'Illuminate/Foundation/Auth/Admin' not found in c:/S/htdocs/i-V/app/Models/Admin.php on line 13
How do I resolve that error?
Admin
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\Admin as Authenticatable;
use Illuminate\Notifications\Notifiable;
use App\Notifications\AdminResetPasswordNotification;
class Admin extends Authenticatable
{
use HasFactory, Notifiable;
protected $guard = 'admin';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
I have tried to remove the composer.lock file then install it again
also I have done this.
composer dump-autoload
composer install --no-scripts
composer update
Change this
use Illuminate\Foundation\Auth\Admin as Authenticatable;
to
use Illuminate\Foundation\Auth\User as Authenticatable;
as Illuminate\Foundation\Auth\User it is core code from laravel core and it don't have Admin class

How to extend multiple things for User.php

I am new to coding and I am trying to extend \TCG\Voyager\Models\User and Authenticatable implements MustVerifyEmail together but I do not know how to do it.
I have tried class User extends \TCG\Voyager\Models\User, Authenticatable implements MustVerifyEmail together but that doesn't work for me.
I have created 2 seperate classes and that does not let me either.
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements MustVerifyEmail extends \TCG\Voyager\Models\User
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
You can't extend two (or more) classes in PHP, just one. A solution could be:
Class A extends Class B
Class B extends Class C
This way, Class A would also be extending Class C (through Class B).
In your case:
User extends \TCG\Voyager\Models\User and implements MustVerifyEmail
\TCG\Voyager\Models\User extends Authenticatable
This is one of the reasons, Traits were introduced in the PHP world (are not the same though).

BadMethodCallException App\User::create() when seeding with zizaco

I'm having an error when seeding to the database using laravel 5.5 the error message is below and there is my users class and my seeder class. What is happening is that one record is being inserted at a time when calling db:seed but after the first call it says BadMethodException rest below
[BadMethodCallException]
Call to undefined method App\User::create()
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Zizaco\Entrust\Traits\EntrustUserTrait;
use Eloquent;
class User extends Eloquent
{
use EntrustUserTrait;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
<?php
use App\User;
use Faker\Factory as Faker;
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
foreach (range(1, 100) as $index) {
$faker = Faker::create();
$user = User::create([
'name' => $faker->firstName . ' ' . $faker->lastName,
'email' => $faker->email,
'password' => bcrypt('secret')
]);
}
}
}
Your user model should extend
\Illuminate\Database\Eloquent\Model
Or
\Illuminate\Foundation\Auth\User
You are extending Eloquent
laravel/laravel repository: https://github.com/laravel/laravel/blob/master/app/User.php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
Your User class needs to extend to Model class
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
...
}
EDIT
This is my User
<?php namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
protected $table = 'users';
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];
}
Hope it helps!

Categories