dmSherazi dmSherazi - 1 month ago 11
PHP Question

laravel jsValidation error while using unique rule

I am having issue while using laravel unique rule in CreateRequest with JsValidation. Here is the Request code.

class CreateProductRequest extends Request
{

public function rules()
{
return [

'factory_id' => 'required',
'category' => 'required',
'product_code' => 'required|unique:product',
'un_code' => 'required|unique:product',
'hs_code' => 'required|unique:product',
'section' => 'required',
'status' => 'required',
'product_type' => 'required',
'classification' => 'required',
'main_product_market' => 'required',
'custodian_user_id' => 'required'

];
}
}


When I remove the unique field its fine. But with unique rules I would get 500 server error as follows
enter image description here

In console it reports

enter image description here

It worth noting that if I don't use the JSValidation Laravel will produce no errors

Update :

In the Debug Bar I am getting this error

'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'name'' in /.../demo/vendor/laravel/framework/src/Illuminate/Database/Connection.php:408:

Answer

The issue was later solved. My issue was that I was not typeHinting the CreateProductRequest and was instead using Request.

I should have been typeHinted the extended Request class that declares the rules.