我的规则如下。请检查以下代码中的composite_unique
public function rules()
{
return [
'SubCategory' => 'required|max:25|min:5|composite_unique:tblsubcategory,CategoryID',
'CategoryID' => 'required|min:1'
];
}
我的更新方法如下
public function update(SubCategoryRequest $request)
{
$SubCat = $this->CacheCollection->getAllSubCategories($request->input('CategoryID'));
$SubCategory = $SubCat->SubCategories->where('SubCategoryID', 1)->first();
$SubCategory->SubCategory = $request->input('SubCategory');
$SubCategory->CategoryID = $request->input('CategoryID');
$SubCategory->save();
}
我的Validator类如下所示。请检查下面代码中的composite_unique规则
class ValidationServiceProvider extends ServiceProvider
{
public function boot() {
$this->app['validator']->extend('composite_unique',
function ($attribute, $value, $parameters, $validator) {
// remove first parameter and assume it is the table name
$table = array_shift( $parameters );
// start building the conditions
$fields = [ $attribute => $value ];
while ( $field = array_shift( $parameters ) ) {
$fields[ $field ] = 'Request::get( $field );
}
// query the table with all the conditions
$result = 'DB::table( $table )
->select( 'DB::raw( 1 ) )
->where( $fields )->first();
return empty( $result ); // edited here
});
}
}
出了什么问题
当我尝试更新记录而不编辑任何内容并点击更新按钮时。。。我得到了一个错误,重复SubCategory和CategoryID的组合。我认为验证代码只是为了在插入新记录之前进行检查。对于更新,它不起作用。
下面是子类别表的架构
CREATE TABLE `tblsubcategory` (
`SubCategoryID` int(11) NOT NULL,
`SubCategory` varchar(25) NOT NULL,
`CategoryID` int(11) NOT NULL,
`IsActive` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `tblsubcategory`
MODIFY `SubCategoryID` int(11) NOT NULL AUTO_INCREMENT;
下面是唯一关键约束
ALTER TABLE `tblsubcategory`
ADD PRIMARY KEY (`SubCategoryID`),
ADD UNIQUE KEY `UK_tblSubCategory_SubCategory_CategoryID` (`CategoryID`,`SubCategory`);
我在ValidationServiceProvider
类内的引导函数中进行了一些操作。以下是所做的工作
class ValidationServiceProvider extends ServiceProvider
{
public function boot() {
$this->app['validator']->extend('composite_unique',
function ($attribute, $value, $parameters, $validator) {
$table = array_shift( $parameters );
$fields = [ $attribute => $value ];
$columnName = null;
$columnValue = null;
while ( $field = array_shift( $parameters ) ) {
if(strpos($field, '#') !== false) {
$columnName = str_replace("#", "", $field);
$columnValue = 'Request::get( $columnName );
}
else
$fields[ $field ] = 'Request::get( $field );
}
if($columnName == null && $columnValue == null) {
$result = 'DB::table( $table )
->select( 'DB::raw( 1 ) )
->where( $fields )->first();
}
else {
$result = 'DB::table( $table )
->select( 'DB::raw( 1 ) )
->where( $fields )
->whereNotIn($columnName, [$columnValue])
->first();
}
return empty( $result ); // edited here
});
}
}
最后是rules
函数
public function rules()
{
return [
'SubCategory' => 'required|composite_unique:tblsubcategory,CategoryID,#SubCategoryID#',
'CategoryID' => 'required|min:1'
];
}
现在让我解释一下上面代码中发生了什么
在规则函数中,您可以检查最后一个逗号分隔的值是#SubCategoryID#
。这是因为我的查询如下。
Select SubCategoryID from tblSubCategory
Where (CategoryID = ? and SubCategory = ?)
and SubCategoryID not in(?)
通过这种方式,我开始知道在whereNotIn
块中放置什么列。所以,在我的例子中,它是whereNotIn
中的SubCategoryID值。最后,在验证函数中,#将从列Name中删除。