Laravel Unique Depends on Other Column - laravel

I have a table school_name and in form i want to apply 2 conditions if school name exist than branch must be unique
I tried below validation but its not working
'name' => 'required',
'branch' => 'required:unique:school_name,name',

You can use Rule::unique to achieve your validation rule
use Illuminate\Validation\Rule;
$schoolname = "abc";
$branch = "new";
Validator::make($data, [
'branch' => [
'required',
Rule::unique('tablename')->where(function ($query) use($schoolname ,$branch) {
return $query->where('school_name', $schoolname)->where('branch', $branch);
});
],
]);

Related

laravel unique field validation rule on update

My property listing form contains a unique reference id , while adding it works fine, but while editing on save i get an error --- "The reference has already been taken".
public $validationRules = [
'property_type_id' => 'required',
'property_category_id' => 'required',
'referance' => 'required|min:5|unique:properties',
];
How to write the rules for update
http://prntscr.com/vyraod
While update you need not to compare with self so exclude that row like this
public $validationRules = [
'property_type_id' => 'required',
'property_category_id' => 'required',
'referance' => 'required|min:5|unique:properties,'.$property->id,
];
While update i set the following validation rules.
Syntax - 'required|min:5|unique:< table name >,< unique field name >,'.$id
public function rules($id)
{
return [
'property_type_id' => 'required',
'property_category_id' => 'required',
'referance' => 'required|min:5|unique:properties,referance,'.$id
];
}
public function update(Request $request, $id)
{
$validator = Validator::make($request->all(), $this->rules($id));
}

Laravel : Unique validation with different fields

I have a table states and fields are id,country_id,state_code,state_name.Now I want to validate that same country doesn't have a same state_code and same state_name in a table.
I tried but it's not working.
In my Controller :
$validator = State::validator($request->all(), $id);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator->getMessageBag())
->withInput($request->all());
}
Here is my validation function in model :
protected function validator(array $data, $id)
{
return Validator::make($data, [
'country_id' => 'required',
'state_code' => 'required',
'state_name' => 'required',
]);
}
How can I solved this without custom validation ?
First of all, your validation rules are only checking if these fields are present in the request. There is no validation happening for a value exists in database. First of all, you might wanna go through the documentation for the rule_exists. Secondly, you may have to update the query for this rule as per the documentation
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::exists('staff')->where(function ($query) {
$query->where('account_id', 1);
}),
],
]);
Here you can pass additional query parameters.
You do not need a custom validation for that one, laravel has a unique column validation
$this->validate($request, [
'country_id' => 'required',
'state_code' => 'required|unique:states,state_name',
'state_name' => 'required|unique:states,state_code',
]);
This way you know for sure that column values in state_code will be unique compare to state_name for more check the docs

how to make validation for two columns

I need a clean solution that's why I'm posting this.
So I have a formrequest validation which is the following:
public function rules()
{
return [
'restaurant_id' => 'required',
'rating' => 'required',
'comment' => 'required',
'visited_at' => 'required|date'
];
}
in controller, I also have user_id but request doesn't contain it. Now, I want to change the above rules so that user_id and restaurant_id both together will only be created only once. So for example . User_id 1 and restaurant_id 2 got saved as a row. If the same values come for these two columns, it should throw an error.
What would be the best and clean way?
You can expand the exists rule like this:
public function rules()
{
$user = $this->user();
return [
'restaurant_id' => [
'required',
Rule::exists('YOUR_TABLE_NAME')->where(function ($query) use ($user) {
$query->where('restaurant_id', request('restaurant_id'))
->where('user_id', $user->id);
})
],
'rating' => 'required',
'comment' => 'required',
'visited_at' => 'required|date'
];
}

Laravel 5.6 make an easy validation on multiple input fields

I am new to learning Laravel and still trying to learn beautiful coding.
I have a code like this that validates the input fields upon submitting the update form and it has many unnecessary codes that can be modified to make it short and better.
So my question is, what is an approach or technique should I take to rewrite my codes to make it short and beautiful.
public function update(Request $request, $id)
{
$product = Product::find($id);
if ($product->name != $request->name AND $product->sku != $request->sku AND $product->description != $request->description) {
$this->validate($request, [
'name' => 'required|unique:products,name',
'sku' => 'required|unique:products,sku',
'description' => 'required'
]);
} elseif ($product->name != $request->name AND $product->sku != $request->sku) {
$this->validate($request, [
'name' => 'required|unique:products,name',
'sku' => 'required|unique:products,sku'
]);
} elseif ($product->name != $request->name AND $product->description != $request->description) {
$this->validate($request, [
'name' => 'required|unique:products,name',
'description' => 'required'
]);
} elseif ($product->description != $request->description AND $product->sku != $request->sku) {
$this->validate($request, [
'description' => 'required',
'sku' => 'required|unique:products,sku'
]);
} elseif ($product->name != $request->name) {
$this->validate($request, [
'name' => 'required|unique:products,name'
]);
} elseif ($product->description != $request->description) {
$this->validate($request, [
'description' => 'required'
]);
} elseif ($product->sku != $request->sku) {
$this->validate($request, [
'sku' => 'required|unique:products,sku'
]);
} else {
return redirect('products/' . $product->id)->with('info', 'Product does not changed!');
}
}
Looks like you have two issues that you need to address to shorten your code:
1) The product name and sku fields are required but you want to avoid collisions with the values that exist on the product that you plan to update.
2) The description field isn't actually required but when it is present it must be not be validated. (shouldn't there be a min:1 validation or something?)
So to solve #1 you need to write your own unique rule to ignore the current $product which you are trying to update when determining a unique value. See Forcing A Unique Rule To Ignore A Given ID -- Laravel Docs
And to solve #2 you can use the sometimes validation Conditionally Adding Rules -- Laravel Docs
Combining those should allow you to use a single validation statement like this:
use Illuminate\Validation\Rule;
...
$this->validate($request, [
'name' => [
'required',
Rule::unique('products')->ignore($product->id)
],
'sku' => [
'required',
Rule::unique('products')->ignore($product->id)
],
'description' => Rule::sometimes('description', 'required', function($input){
return $input->description !== $product->description;
});
]);
If this is not correct please, update this answer for the next person who stumbles by. Hope this helps.
[Tip:] It seems like a long shot but if the description field is a WYSIWYG field on the client interface I have had them hit the max length of a text field as they can convert images into base64, so even although it's unlikely for most situations I try to incorporate max:65535 in the validation so that it does gracefully fail. (65535 is the max length of a MySQL text field other databases are different sizes)

i want each email to be unique for a certain field's id . how is it possible for validation in laravel 5

laravel 5
In laravel 5 form validation I need email to be only unique for a certain field's id in the db table.
$this->validate($request,[
'name'=>'required',
'email' => 'unique:users,email,',
'password'=>'required',
]);
You can try this way:
$companyId = $request->get('company_id'); // selected company
$this->validate($request,[
'name' => 'required',
'email' => [
'required',
"unique:users,email,NULL,id,company_id,$companyId"
],
'password'=>'required',
]);
If you want to ignore a specific id in the database, you can use ignore. For example, when a user is updating his profile, we want to allow him to send his email address, it should not be considered a not unique.
Here is the docs details. And here is your code updated to match that:
$this->validate($request,[
'name'=>'required',
'email' => [
\Illuminate\Validation\Rule::unique('users', 'email')->ignore($user->id),
],
'password'=>'required',
]);
However, if want to ignore more than one id or want to customize the query, you need to add a where clause:
$this->validate($request,[
'name' => 'required',
'email' => [
'required',
\Illuminate\Validation\Rule::unique('users')->where(function ($query) {
// Add any constraints you want for the query here.
return $query->whereNotIn('id', [1, 2, 3, 4]);
}),
],
'password'=>'required',
]);
Use Third and Fourth param to ignore for company_id
$company_id = 1;// Company will be here
And in validation
'email' => 'unique:users,email,'.$company_id.',company_id',

Resources