我在Laravel 5.1中尝试以下查询
Select * from tblSubCategory
Where SubCategoryID != 1 and CategoryID = 1 and SubCategory = 'test data';
以下是我在Laravel中的代码
$v = \Validator::make($request->all(), [
'SubCategory' => 'required|max:100|min:5|unique:tblsubcategory,SubCategory,'
.''.$request->input('CategoryID')
.',CategoryID,SubCategoryID,'.$request->input('SubCategoryID')
]);
以上表达式产生以下查询。
select count(*) as aggregate from `tblsubcategory`
where `SubCategory` = 'under website1' and `CategoryID` <> 1 and `SubCategoryID` = 1
唯一键
UNIQUE KEY `UK_tblSubCategory_SubCategory_CategoryID` (`CategoryID`,`SubCategory`);
答案 0 :(得分:1)
在命令行:composer require felixkiss/uniquewith-validator:2.*
将以下内容添加到providers array
中的 config/app.php
:
'providers' => array(
'Felixkiss\UniqueWithValidator\UniqueWithValidatorServiceProvider',
),
$v = \Validator::make($request->all(), [
'SubCategory' => 'required|max:100|min:5|unique_with:tblsubcategory,CategoryID',
'CategoryID' => 'required',
]);
$v = \Validator::make($request->all(), [
'SubCategory' => 'unique:tblsubcategory,SubCategory,'
.$request->input('SubCategoryID')
.',SubCategoryID,CategoryID,'
.$request->input('CategoryID')
]);
if ($v->fails()) {
return \Redirect::back()
->withErrors($v)
->withInput();
}