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;
Related
How to add id in stud_num just like in email and username? the codes found in User Controller.
public function update(Request $request, $id)
{
$this->validate($request, [
'first_name' => 'required|max:255|regex:/^([^0-9]*)$/',
'middle_name' => 'nullable|max:255|regex:/^([^0-9]*)$/',
'last_name' => 'required|max:255|regex:/^([^0-9]*)$/',
'contact' => ['required', 'regex:/^(09|\+639)\d{9}$/'],
'course' => 'required',
'role_as' => 'required',
'stud_num' => ['required', 'unique:users,stud_num', 'max:15', new StrMustContain('TG')],
'username' => 'required|alpha_dash|unique:users,username,' . $id,
'email' => 'required|email:rfc,dns|unique:users,email,' . $id
]);
// codes for update
}
Just add id like email and username.
'stud_num' => ['required', 'unique:users,stud_num,'.$id, 'max:15', new StrMustContain('TG')]
you can write it like this:
'stud_num'=>['required',Rule::unique('users','stud_num')->ignore($id),'max:15',new StrMustContain('TG')]
I want to update record but before update i want to check :-
1. if user exists at time not allowed to update.
Example :-
User jay try to update his name, but user insert same name which is already exists in jay field. at time i want to allow user to update but this code give error that username already exists. What i should do ??
Controller :-
public function update_data($update_id){
$gender_list = ['Male', 'Female', 'Other'];
$country_list = ['India', 'US', 'UK', 'Germany', 'Austraila'];
$Validator = $this->validate(request(), [
'username' => 'required|unique:userlists|alpha_num|max:30',
'email' => ['required', 'unique:userlists', 'email', 'regex:/((yahoo|gmail|hotmail)\.com)/'],
'password' => 'required',
'bod' => 'required|after_or_equal:today',
'comments' => 'required',
'phone_no' => 'required|numeric',
'country' => 'required|alpha',
'gender' => 'required',
'agreement' => 'required',
],[
'required' => 'Please Enter Your :attribute',
]);
if(!in_array($request->gender, $gender_list) || !in_array($request->country, $country_list)){
session()->flash('G_msg', 'Hello Hackes Please Go Back');
return back();
}
if($Validator->passes()){
// updation code.
}
}
You can use validation like below:
'username' => ['required','alpha_num', 'max:30',Rule::unique('userlists')->ignore($update_id)]
Make sure Rule is defined above Class:
use Illuminate\Validation\Rule;
try unique
'username' => 'required|unique:table_name,username,' . $update_id . ',user_id',
You may use Rule
use Illuminate\Validation\Rule;
'username' => ['required','alpha_num', 'max:30',Rule::unique('userlists')->ignore($update_id)]
I want to do "unique" validation on multiple fields. I have written below validation rule but not sure how to include brand_id and company_id in it
$request->validate([
'name' => 'required|string|unique:products',
'company_id' => 'required',
'brand_id' => 'required',
]);
So what I am trying to do is, ADD PRODUCT but check if the name is unique for the given company_id and brand_id. How can I do that?
We can use Rule::unique() function to add custom conditions to unique validation.
$request->validate([
'company_id' => 'required',
'brand_id' => 'required',
'name' => [
'required',
'string',
Rule::unique('products')->where(function($query) {
$query->where('company_id', request('company_id'));
$query->where('brand_id', request('brand_id'));
}),
],
]);
Sometimes you may wish to stop running validation rules on an attribute after the first validation failure. To do so, assign the bail rule to the attribute:
$request->validate([
'name' => 'bail|required|string|unique:products',
'company_id' => 'bail|required',
'brand_id' => 'required',
]);
You can add additional wheres after the ignore options (the NULL,id below), e.g.:
$request->validate([
'company_id' => 'required',
'brand_id' => 'required',
'name' => 'required|string|unique:products,name,NULL,id,company_id,'.request('company_id').',brand_id,'.request('brand_id'), '
]);
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'],
]);
}
I have a users table which has a unique validate rule on email and username. When i am trying to update not ignore unique validation. Please see my code below.
UserRequest.php
public function rules()
{
return [
'name' => 'required',
'mobile' => 'required',
'email' => 'required|unique:users,email'.$id,
'usercategory' => 'required',
'username' => 'required|unique:users,username'.$id,
];
}
Please try this
public function rules()
{
$id = $this->request->get('id') ? ',' . $this->request->get('id') : '';
return [
'name' => 'required',
'mobile' => 'required',
'email' => 'required|unique:users,email'.$id,
'usercategory' => 'required',
'username' => 'required|unique:users,username'.$id,
];
}
In laravel 5.5 you should do like this:
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
Check laravel documentaion about rule-unique.
You needed to skip id if you validate for update, like as below
public function rules($id='')
{
$id = $id ? ','.$id.',id':'';
return [
'name' => 'required',
'mobile' => 'required',
'email' => 'required|unique:users,email'.$id,
'usercategory' => 'required',
'username' => 'required|unique:users,username'.$id,
];
}
In Laravel docs, you have provide 3rd and 4th param in unique rule
unique:table,column,except,idColumn
You could use something like this:
$id = $this->isMethod('put') ? ',' . auth()->id() : '';
assuming you use put method for update
before line with return
for somebody else faced this issue:
public function rules()
{
return [
'name' => 'required',
'mobile' => 'required',
'email' => 'required|unique:users,email'.$this->user->id,
'usercategory' => 'required',
'username' => 'required|unique:users,username'.$this->user->id,
];
}
I fixed this by using this:
public function rules()
{
$id = $this->user->id ?? null;
return [
"name" => "required",
"mobile" => "required",
"email" => "required|unique:users,email, $id",
"usercategory" => "required",
"username" => "required|unique:users,username, $id",
];
}
Note that for other models other than the User model, the user in $this->user->id will be the model name in lowercase