I need to know the way to send the error by the controller or know if there is another option to display the error.
VehicleYearController.php
public function store(Request $request)
{
$this->validate(
$request,
[
'v_year' => 'required|max:4|min:4'
],
[
'v_year.required' => 'El campo año de inicio es obligatorio',
'v_year.min' => 'El campo año de inicio debe tener al menos 4 caracteres',
'v_year.max' => 'El campo año de inicio debe tener a lo más 4 caracteres'
]
);
$years = DB::table('vehicle_years')->where([
['v_id', '=', $request->v_id],
['v_year', '=', $request->v_year]
])->get();
if (! $years->isEmpty()) {
//HERE I NEED TO SHOW THE ERROR THAT THIS DATA ALREADY EXISTS
} else {
$data = $request->all();
VehicleYear::create($data);
}
}
$validator->errors->add('key', "error message");