How to validation a unique column on update in laravel?

I have a “vehicles” table with a unique column called “vehicle_number.” When adding a new vehicle, I can easily validate if the “vehicle_number” is unique.

$request->validate([
    'vehicle_number'    => 'required|unique:vehicles|max:255'
]);

However, when I try to update a vehicle, I encounter an issue where it says “vehicle_number” already exists, preventing any updates. How can I set up validation in Laravel to allow the same “vehicle_number” during the update?

You can add the current record ID in the validation rule to allow the same “vehicle_number” for the current record during updates. Here’s how to set up the validation:

$request->validate([
    'vehicle_number' => 'required|max:255|unique:vehicles,vehicle_number,' . $vehicle->id,
]);

Make sure you are using the record primary key {id}