I have no idea how to hash fillable password input. I'm trying to hash it then gets stored to the database. Here's what I've done so far
use App\User;
use Illuminate\Http\Request;
class RegistrationController extends Controller
{
public function store()
{
$this->validate(request(), [
'name' => 'required',
'email' => 'required|email',
'password' => 'required|confirmed'
]);
$pass = bcrypt(request()->password);
$user = User::create(request(['name', 'email', $pass]));
auth()->login($user);
return redirect()->home();
}
}
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password'
];
protected $hidden = [
'password', 'remember_token',
];
}
It gives me a QueryException:
SQLSTATE[HY000]: General error: 1364 Field 'password' doesn't have a default value (SQL: insert into users (name, email, updated_at, created_at) values (the name i inserted, email inserted, date timestamp, date timestamp)
When using: $user = User::create(request(['name', 'email', $pass]));, you are passing an array to the request method and one of the elements ($pass) is not a key of $request.
I believe it should look more like:
$user = User::create([
'name' => request('name'),
'email' => request('email'),
'password' => $pass
]);
EDIT
Also, remember that Laravel provides a Hash facade to help you with encryption:
You can also use the Hash facade to do the same as bcrypt.
Thanks to lagbox for the correction.
$hashedPassword = Illuminate\Support\Facades\Hash:make(request('password'));
Related
My code is:
$user = User::create([
'email' => $request->email,
'password' => Hash::make($request->password),
]);
$user_details = [
'name' => $request->name,
'address' => $request->address,
'lastname' => $request->lastname,
'secondname' => $request->secondname,
'inn' => $request->inn,
'fincode' => $request->fincode,
];
$user->details()->create($user_details);
Model User is:
class User extends Authenticatable implements JWTSubject
{
use Notifiable;
public function details()
{
return $this->hasOne(UserDetails::class, 'user_id', 'id');
}
}
UserDetails model is:
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserDetails extends Model
{
protected $table = 'enterprise';
protected $fillable = ['name', 'lastname', 'secondname', 'address', 'inn', 'fincode'];
}
I believe that your $user has not been persisted, hence having the error, in your case $user is null, that's why you cannot call details on null object. Make sure that you use all the required fields on your user.
You might be missing the fillable array in the User model if the one that you shared is the full content, then add this:
protected $fillable = [ 'email', 'password'];
I am using CREATE method for mass assignment in laravel eloquent, the result also printed and showing, but when i check table in database, no data inserted.
I am using laravel 5.6, It showing successful, but in real no data saved in table.
here is my code
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
protected $table = 'users';
public $primaryKey = 'user_id';
public $timestamps = false;
protected $fillable = [
'first_name','middle_name', 'last_name', 'role_id', 'email', 'mobile', 'telephone'
];
.... and other code
Here is my insert function
$users = User::create([
'first_name' => $row[1],
'middle_name' => $row[2],
'last_name' => $row[3],
'email' => $row[4],
'role_id' => 6,//$row[5],
'telephone' => $row[6],
'mobile' => $row[7],
]);
var_dump($users);// this shows result.
$users prints data. There is no error showing. BUT when i check database, table is empty. I expect the same data in users table.
I'm using Laravel 5.5 and I have modified the registration process recently. This is the registration:
$user = User::create([
'gender' => $data['gender'],
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'status' => 'inactive',
'password' => Hash::make($data['password'])
]);
which works fine so far. However, when I try to log in with the account I recently had created, it'd always tell me that was the wrong password and I don't understand why. I'm not exactly sure whether it was Hash::make() or bcrypt() originally and since they're different, I suppose there must be something wrong with the login.
If I bcrypt a password using Tinker and insert it into the DB, I'm able to login. I thought of modifying the "login code", however, I wouldn't find anything similiar to the registration looking process which would let me alter the used hash algorithm for login, so what's the approach in this case?
Edit: I placed this code above the $user = User::create([]):
dd($data['password'], bcrypt($data['password']), Hash::make($data['password']));
and this is the output:
"testtesttest123456"
"$2y$10$y9bl5muW5AmmMZMMEWL0Qucy7RSfCSzgWXl29PiX2gPRFd3jnNeEC"
"$2y$10$tez1W8fIwpksgpjsZmQqPuYIN4QTtiddhaCnc5zQ2MgeYATiQd9Ym"
The user model (as per request):
class User extends Authenticatable
{
use Notifiable, HasRoles;
const STATE_ACTIVE = 'active';
const STATE_INACTIVE = 'inactive';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'first_name', 'last_name', 'email', 'password', 'status', 'profile_pic', 'api_token', 'activation_code'
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function setPasswordAttribute($password)
{
$this->attributes['password'] = bcrypt($password);
}
public function setEmailAttribute($email)
{
$this->attributes['email'] = strtolower($email);
}
}
You are double hashing the password. You have a mutator that is hashing the password. You are also hashing the password before passing it to create.
You have the choice of removing that mutator or not hashing the password before assigning it to the model (as assigning it causes the mutator to run). Either one is fine you just have to know which way you are going.
Most likely there is only going to be 2 places a password is getting hashed anyway, so its really not that big of a concern which way you go.
Registration and perhaps a change password/profile type route are basically the only places you will be hashing the password.
If you haven't done anything to the login/registration hasing in the controllers then this should work
$user = User::create([
'gender' => $data['gender'],
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'status' => 'inactive',
'password' => bcrypt($data['password'])
]);
I'm trying to find out why when I dd($request->all()) in the store method of my controller everything is correct, however when I send it to the model function register() its no where to be seen.
I'm not quite sure what I'm doing wrong.
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class UsersController extends Controller
{
public function store(Request $request, User $user)
{
$this->authorize('delete', $user);
$this->validate($request, [
'firstName' => 'required|min:3',
'lastName' => 'required|min:3',
'displayName' => 'required|min:3',
'emailAddress' => 'required|email|unique:users,email',
'password' => 'required|min:3',
'role' => 'required|exists:roles,id'
]);
$userRegistered = $user->register(
new User($request->all())
);
if ($userRegistered) {
flash()->success('Success', 'The user has been successfully created!');
} else {
flash()->error('Error', 'The user could not be successfully created!');
}
return redirect()->to(route('users'));
}
}
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Authenticatable
{
use SoftDeletes;
/**
* Fillable fields for a user.
*
* #var array
*/
protected $fillable = [
'first_name',
'last_name',
'display_name',
'email',
'password',
'role_id'
];
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function register(User $user)
{
return $user->create([
'first_name' => $user->firstName,
'last_name' => $user->lastName,
'display_name' => $user->displayName,
'email' => $user->emailAdress,
'password' => $user->password,
'role_id' => $user->role
]);
}
}
You've mixed up the formatting of your variables between your request data and your User model.
According to your validation logic, the request data is coming is as camelCase. Yet, according to your $fillable array, the fields on your User model are snake_case. But, even then, in your register method, you're attempting to access the fields on the User model using camelCase.
You haven't given enough information for a definitive answer, but you need to fix the formatting of your variables. For example, change your request fields names from camelCase to snake_case, and make sure you access your fields on the model using snake_case.
You have to pass a list of attributes to "validate" method.
//...
$this->validate($request->all(), [
'firstName' => 'required|min:3',
'lastName' => 'required|min:3',
'displayName' => 'required|min:3',
'emailAddress' => 'required|email|unique:users,email',
'password' => 'required|min:3',
'role' => 'required|exists:roles,id'
]);
One more thing..check if you are using "web" middleware. (Kernel.php => MiddlewareGroups). Add that middleware to your route.
I have a user table with a referral column and this code in controller/auth:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'referral' => md5($data['email']),
]);
}
It adds a name, email and password but not referral.
There is no error or notice. What should I do to fill also referral column?
Open your User model (probably app/User.php, and change:
protected $fillable = [
'name', 'email', 'password',
];
into
protected $fillable = [
'name', 'email', 'password', 'referral'
];
You need to add column to fillable to save it.