One column update query is not performing. the added data is not updating in the database.
my view:
<?php echo Form::open(array('url' => 'admin/add-edit-spe')) ;?>
/* form elements */
Admincontroller
public function postAddEditSpe()
{
Input::flash();
$rules = array(
'spe_name' => 'required|unique:spec',
'description' => 'required'
);
$messages = array(
'spe_name.required' => 'Spe name is required',
'spe_name.unique:' => 'Spe name should be unique',
'description.required' => 'Spe description is required',
);
$validator = Validator::make(Input::all(), $rules, $messages);
if ($validator->fails())
{
dd($validator->errors);
/* shows the same page */
}
else{
$data['spe_id']=Input::get('spe_id');
$data['spe_name']=Input::get('spe_name');
$data['spe_content']=Input::get('description');
$data['r_tags']=Input::get('r_tags');
$data['f_spe']=Input::get('f_spe');
$data['r_spe']=implode(",",Input::get('r_id'));
DB::table('spec')->where('spe_id','=',$data['spe_id'])->update(array('spe_name' => $data['spe_name'],'spe_content' => $data['spe_content'],'f_spe' => $data['f_spe'],'related_spe' => $data['r_spe'],'r_tags' => $data['r_tags']));
}
}
the validation error shows :
Undefined property: Illuminate\Validation\Validator::$errors
the output of dd($validator->messages());
is:
object(Illuminate\Support\MessageBag)#895 (2) { ["messages":protected]=> array(1) { ["spe_name"]=> array(1) { [0]=> string(43) "The spe name has already been taken." } } ["format":protected]=> string(8) ":message" }
the else part is not executed becuase of validation failure. can anyone tell me what this validation error means? why it occurs?
Edition i made
I re-arranged the validation rules for insert and update like this:
if(Input::get('speciality_id')!=0){
// Updation
$rules = array(
'speciality_name' => 'required|exists:speciality',
'description' => 'required'
);
}
else{
// Insert
$rules = array(
'speciality_name' => 'required|unique:speciality',
'description' => 'required'
);
}
In this case updation is working, insertion are working. but only one case is not satisfying it is "if the db contains an entry with name "abc" and "xyz". we will check whether these are unique while inserting. but if i select "xyz" for Edit and changes its name to "abc" and click update. two "abc" will occur. the "exists" rule satisfied. This scenario must not happen. What should i do to avoid this test case.??
For updation use this rule . Pass current row id to rules . it will do the charm
$rules = array(
'spe_name' => 'required|unique:spec,:id',
'description' => 'required'
)
For more information check