I am doing an ajax login in my Laravel application. It worked fine on my local environment, but I moved the site to Production, on login I see this exception
ErrorException in User.php line 29: Illuminate\Foundation\Auth\User
and Illuminate\Auth\Authenticatable define the same property
($rememberTokenName) in the composition of App\User. This might be
incompatible, to improve maintainability consider using accessor
methods in traits instead. Class was composed
My User model is as
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable, \Illuminate\Auth\Authenticatable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password', 'first_name', 'last_name', 'hospital_id', 'census_id', 'employee_id'
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
Illuminate\Foundation\Auth\User already uses use Illuminate\Contracts\Auth\Authenticatable, so you basically applying the trait twice which causes that error.
Here's the code to Illuminate\Foundation\Auth\User.php
<?php
namespace Illuminate\Foundation\Auth;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements
AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
}
Also a side suggestion rename your alias bit as Authenticable maybe to something more clear like AuthUser / BaseUser so it's clear you are extending a User class.
Related
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).
I have used this code
User.php file
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
protected $guarded = ['role'];
protected $hidden = [
'password', 'remember_token',
];
}
?>
Error: FatalThrowableError in User.php line 7: Class
'App\Authenticatable' not found
How can I solve it?
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $guarded = ['role'];
protected $hidden = [
'password', 'remember_token',
];
}
The class that Laravel uses is Illuminate\Foundation\Auth\User which they alias as Authenticatable.
At the top of your user model, you can import this base class which the default User model extends:
use Illuminate\Foundation\Auth\User as Authenticatable;
This base class implements the standard interfaces and uses the standard traits (including the one named Authenticatable) for you.
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;
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!
I am currently writing an application that is using Laravel 5.3 on the backend, and I'm looking for a way to overwrite the default password reset behaviour.
The class that I need to change is "ResetPassword" located here:
/Illuminate/Auth/Notifications/ResetPassword.php
Reason for the change is, that the reset url generated in this file is not correct for my front-end - as it uses url(), which puts the API url rather then the front-end url in the reset email.
You can override CanResetPassword's sendPasswordResetNotification() method in your User.php
use Illuminate\Notifications\Notifiable;
use App\Notifications\CustomResetPasswordNotification;
public function sendPasswordResetNotification($token)
{
$this->notify(new CustomResetPasswordNotification($token));
}
and create CustomResetPasswordNotification.php according your requirements.
Check Password Reset Emails section here for more details
I found a quick and easy way to overwrite the password reset process by overwriting the User class located here:
/Illuminate/Foundation/Auth/User.php
Basically, I created my own version like this:
<?php
namespace App\Traits\Auth;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use App\Traits\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements
AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
}
I saved it to /App/Traits/Auth and now use it in my User model.
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use App\Traits\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',
];
}
Once finished, you can create your own version of the "CanResetPassword" trait and replace the Notification class and make necesarry adjustments.
Here is an example replacement for the "CanResetPassword" trait:
namespace App\Traits\Auth\Passwords;
use App\Notifications\CustomResetPassword as ResetPasswordNotification;
trait CanResetPassword
{
/**
* Get the e-mail address where password reset links are sent.
*
* #return string
*/
public function getEmailForPasswordReset()
{
return $this->email;
}
/**
* Send the password reset notification.
*
* #param string $token
* #return void
*/
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPasswordNotification($token));
}
}