I have a table tanks
+----+----------+-------+-------+
| id | capacity | model | width |
+----+----------+-------+-------+
| 1 | 1000 | 15 | 960 |
| 2 | 50000 | 30 | 200 |
| 3 | 100 | 15 | 12 |
| 4 | 80000 | 40 | 100 |
| 5 | 1000 | 30 | 123 |
| 6 | 500 | 5 | 1213 |
| 7 | 1000 | 22 | 2234 |
+----+----------+-------+-------+
And I added the unique property in my table
ALTER TABLE `tanks`
ADD UNIQUE `capacity_model_width` (`capacity`, `model`, `width`);
And my function to store values is like this
public function store(Request $request) {
$image = new tank();
$this->validate($request, [
'capacity' => 'required|numeric',
'model' => 'required|numeric',
'width' => 'required|numeric',
]
);
$image->capacity = $request->capacity;
$image->model = $request->model;
$image->width = $request->width;
$image->save();
return redirect()->route('tank.index')
->with('success', 'Tank created successfully');
}
Now when I insert below error shows
Integrity constraint violation: 1062 Duplicate entry
'1000-22-2234' for key 'capacity_model_width'
I need to show the error message in my submit if they are unique. I am new to Laravel, How can add a custom validation and error message inside the store function