I'm trying to customize Laravel's authentication fields. I succeeded for the "name" and "password" fields, but not for the "email" field. I still have the error:
SQLSTATE[42S22]: Column not found: 1054 "email" field unknown in where
clause.
I tried to rely on this this, but it didn't work. In RegisterController, I changed the create function to the following.
protected function create(array $data)
{
return User::create([
'user_pseudo' => $data['name'],
'user_email' => $data['email'],
'usr_mdp' => bcrypt($data['password']),
]);
}
This error could come from the unique validation of the email field in the validation method. If there's no column name specified it will use the name of the field as the column name.
Add the correct column name to the rule in which it's supposed to search for the email and this error should be gone:
RegisterController.php
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users,user_email'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
Related
I am using Fortify, and I changed the email input field name to user_email and made the necessary changes in the validator as below and register form, but I got an error.
namespace App\Actions\Fortify;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Laravel\Fortify\Contracts\CreatesNewUsers;
use Laravel\Jetstream\Jetstream;
class CreateNewUser implements CreatesNewUsers
{
use PasswordValidationRules;
/**
* Validate and create a newly registered user.
*
* #param array $input
* #return \App\Models\User
*/
public function create(array $input)
{
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
'phone' => ['required', 'string', 'max:255'],
'user_email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'user_password' => $this->passwordRules(),
'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['required', 'accepted'] : '',
])->validate();
return User::create([
'name' => $input['name'],
'phone' => $input['phone'],
'email' => $input['user_email'],
'password' => Hash::make($input['user_password']),
'role' => 5, //1 superadmin and 5 user
'status' => 1,
]);
}
}
Error
Illuminate\Database\QueryException SQLSTATE[42S22]: Column not found:
1054 Unknown column 'user_email' in 'where clause' (SQL: select
count(*) as aggregate from users where user_email =
myemail#xyz.com)
Because you have use user_email validation with unique:users , the validator is trying to execute query as select count(*) as aggregate from users where user_email = "myemail#xyz.com"
Try to remove unique validation or provide your database column name as 'unique:users,email' in email validation.
Could I know how to take the user Id to Request an update? for example, when I updated the user and password. But, except email. At that time, It showed "the message that the email is already taken. When I searched for solutions, I found to solve with the user id. I know this question is asked many times. But, I didn't get any suitable answer for me. Could you help me, please?
This is my Controller Code
public function edit(Users $request,$id){
$users=User::whereId($id)->firstorFail();
$users->name = $request->get('name');
$users->email = $request->get('email');
$users->password = Hash::make($request->get('password'));
$users->role = $request->get('role');
$users->update();
$request->session()->forget('editvalue');
$userdata = User::paginate(4);
// session()->flash('status', 'User has been successfully added.');
return view('pages.auth.register', compact('userdata'))->with('status','User has been successfully added.');
}
This is my Request Form. I want to take id value in this. When I take value, it is showing the message that Trying to get property 'id' of non-object
public function rules() {
return [
'name' => 'required', 'string', 'max:255',
'email' => 'sometimes','required', 'string', 'email', 'max:255', 'unique:users,'. $this->users->id,
'password' => 'required', 'string', 'min:8', 'confirmed',
'role' => 'required', 'string',
];
}
This is my web.php
Route::get('users/edit/{id}', 'UsersController#editscreen');
Route::post('users/edit/{id}', 'UsersController#edit');
You should also put the column name to the rule,
the pattern should be unique:table,column,except_id
Can you replace your RequestForm with this:
public function rules()
{
return [
'name' => ['required', 'string', 'max:255'],
'email' => ['sometimes','required', 'string', 'email', 'max:255', 'unique:users,email,'. $this->users->id],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'role' => ['required', 'string'],
];
} }
I think you can't validate {id} in the request class but you can validate with regex in the route. (My Example is for laravel 8 but the principle remains the same)
Route::post('/users/edit/{id}', [UsersController::class, 'editscreen'])
->where('id', '[0-9]+');
I got with this.
use Illuminate\Validation\Rule;//import Rule class
public function rules()
{
return [
'name' => 'required', 'string', 'max:255',
'email' => ['sometimes','required', 'string', 'email', 'max:255',
Rule::unique('users')->ignore($this->id),
],
'password' => 'required', 'string', 'min:8', 'confirmed',
'role' => 'required', 'string',
];
}
I just encountered this problem and managed to solve by adding $this->id only.
public function rules()
{
return [
'name' => ['required', 'string', 'max:255']
'email' => ['required', 'string', 'unique:users,email,' . $this->id]
];
}
I have a user settings form with 4 fields - first and last name, date of birth and username. The username is unique field in the database. The issue that I run into is when I already have set your username but after that want to update the last name or first name it always throws an error that the username is already in use. Can I somehow check if the username hasn't been changed to not validate it? Only to validate the other fields?
public function update(Request $request)
{
$user = Auth::user();
$this->portfolioValidator($request->all())->validate();
$user->username = $request->username;
$user->contact->first_name = $request->first_name;
$user->contact->last_name = $request->last_name;
$user->contact->save();
$user->save();
return response()->json(['message' => 'The changes have been saved'], 201);
}
protected function portfolioValidator(array $data)
{
return Validator::make($data, [
'first_name' => ['required', 'string'],
'last_name' => ['required', 'string'],
'username' => ['required', 'string', 'min:4', 'max:30', 'unique:users'],
]);
}
You can update your unique rule to ignore the current user as described here:
use Illuminate\Validation\Rule;
protected function portfolioValidator(array $data)
{
return Validator::make($data, [
'first_name' => ['required', 'string'],
'last_name' => ['required', 'string'],
'username' => ['required', 'string', 'min:4', 'max:30', Rule::unique('users')->ignore(Auth::user()->id)],
]);
}
I want to remove validation on the name in laravel 6 on creating a new user. The user is created successfully but when I enter a name with space or capital letters, the login page opens up. But if I remove all spaces from the name everything works fine with the following code.
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
protected function create(array $data)
{
$username = slugify($data['name']) . "-" . mt_rand(10000, 99999);
return User::create([
'name' => $data['name'],
'username' => $username,
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
blade.php code
https://codeshare.io/5e1kX7
Try
//...
'name' => ['required', 'string', 'regex:/^[a-zA-Z0-9\s]+$/', 'max:255'],
//...
I did it by adding regex in the validation
'name' => ['required', 'string','regex:/^[\pL\s\-]+$/u', 'max:255'],
Working with Laravel 5.4, I need to validate if the email field on the users table is unique only if the role_id field is not 999.
In the Register Controller I did this:
return Validator::make($data, [
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'email' => [
'required',
'email',
'max:255',
Rule::unique('users')->where(function($query) {
$query->where('role_id', '<>', 999);
})
],
'phone' => 'required|phone',
'password' => 'required|min:6|confirmed'
]);
But when I try to register I get this error:
Class 'App\Http\Controllers\Auth\Rule' not found
What am I doing wrong?
Thank you
You have to include the Rule class with a use at the top of your file after the namespace declaration:
use Illuminate\Validation\Rule;