php - Issue in Validation when updating the record : Laravel 5.2 -
my rule below. please check composite_unique in below code
public function rules() { return [ 'subcategory' => 'required|max:25|min:5|composite_unique:tblsubcategory,categoryid', 'categoryid' => 'required|min:1' ]; }
my update method below...
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(); }
my validator class below. please check composite_unique rule in below code.
class validationserviceprovider extends serviceprovider { public function boot() { $this->app['validator']->extend('composite_unique', function ($attribute, $value, $parameters, $validator) { // remove first parameter , assume table name $table = array_shift( $parameters ); // start building conditions $fields = [ $attribute => $value ]; while ( $field = array_shift( $parameters ) ) { $fields[ $field ] = \request::get( $field ); } // query table conditions $result = \db::table( $table ) ->select( \db::raw( 1 ) ) ->where( $fields )->first(); return empty( $result ); // edited here }); } }
what's problem
when try update record , don't edit , click on update button... getting error duplicate combination of subcategory , categoryid. think validation code done check before inserting new record. update not working.
below schema of subcategory table
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;
below unique key constraint
alter table `tblsubcategory` add primary key (`subcategoryid`), add unique key `uk_tblsubcategory_subcategory_categoryid` (`categoryid`,`subcategory`);
i did manipulations in boot function inside validationserviceprovider
class. below done.
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 }); } }
and here rules
function
public function rules() { return [ 'subcategory' => 'required|composite_unique:tblsubcategory,categoryid,#subcategoryid#', 'categoryid' => 'required|min:1' ]; }
now let me explain happening in above code
in rules function, can check last comma separated value #subcategoryid#
. because query below.
select subcategoryid tblsubcategory (categoryid = ? , subcategory = ?) , subcategoryid not in(?)
in way getting know column place in wherenotin
block. so, in case, subcategoryid value in wherenotin
. in validation function, # being removed column name.
Comments
Post a Comment