1

In my project I want use validator to check already exist. Works fine for single existence. But in model add I want to check the same model should not exist with same type and make.with diff make or type it will be ok but not with same.

$input['name'] = Input::get('name');
$rules = array('name' => "unique:types,name");
$validator = Validator::make($input, $rules);
if ($validator->fails()) { 
    return Redirect::back()->with('alert-danger',"Type Name already exist.")->withInput();
} else {
    $type = new Type();
    $type_detail = $type->add_type($request);
    $data['name'] = $request->name;
    return redirect('/types')->with('alert-success', 'Type Created successfully.');
}
llanato
  • 2,508
  • 6
  • 37
  • 59
Alfiza malek
  • 994
  • 1
  • 14
  • 36
  • Your question is not clear. Can you give some more information? – Amit Gupta Dec 14 '16 at 13:42
  • please give clarity that what you need to check . – Soniya Basireddy Dec 14 '16 at 13:44
  • 1
    OK. You may achieve this using CustomValidation rules. Please check this SO question: http://stackoverflow.com/questions/26121417/laravel-validation-exists-with-additional-column-condition-custom-validation – Dev Dec 14 '16 at 13:50
  • ex: in city table we add adding state and country .now I want to validate that if the city is entered with already existed city and state and country in city table it should show an error for city already exists.ex i had city records like city-pune,maharastra,india and then i add same city with same record should show an error but allo if i am adding with pune,gujrat,india. i want rule validation for this – Alfiza malek Dec 14 '16 at 13:52

1 Answers1

1

Assuming your $data is as:

$input = [
    'city' => 'pune'
    'state' => 'maharastra'
    'country' => 'india'
]

Then your rules string should be as:

'city' => "unique:table_name,city,NULL,id,state,{$input['state']},country,{$input['state']}",

Docs

Amit Gupta
  • 17,072
  • 4
  • 41
  • 53