在我的项目中,我想使用验证器检查已存在。适用于单身生活。但在模型添加中我想检查相同的模型不应该存在相同的类型和make.with diff make或类型它将是好的但不是相同的。
$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.');
}
答案 0 :(得分:1)
假设您的$data
为:
$input = [
'city' => 'pune'
'state' => 'maharastra'
'country' => 'india'
]
然后你的规则字符串应该是:
'city' => "unique:table_name,city,NULL,id,state,{$input['state']},country,{$input['state']}",