Laravel 5.5:Form Request传递实体ID至唯一验证失效排查
Hey there, let's break down why your update request is triggering that unique validation error even when you're trying to save the same organization—this is a super common gotcha when switching from inline validation in controllers to Form Requests!
The Core Issue
When you weren't using a Form Request, you could directly access the organization's ID from your route parameters and pass it to the ignore() method of the unique rule. But with Form Requests, if you don't explicitly include that ID exclusion in your validation rules, Laravel checks all records in the table—including the one you're trying to update—causing the false positive error.
Common Mistakes & Fixes
Let's walk through the key missteps and how to correct them:
Hardcoded or Missing
ignore()in Form Request Rules
If your Form Request'srules()method looks like this (no exclusion for the current entity):public function rules() { return [ 'name' => 'required|unique:organizations' ]; }Laravel will flag the existing organization's name as a duplicate, even though you're just updating it.
Fix: Use the
Rulefacade to dynamically exclude the current organization's ID. First, import the Rule class, then fetch the ID from your route parameters:use Illuminate\Validation\Rule; public function rules() { // Get the organization ID from your route (adjust the parameter name to match yours) $orgId = $this->route('organization')->id; // Or if your route uses {id} instead: $orgId = $this->route('id'); return [ 'name' => [ 'required', Rule::unique('organizations')->ignore($orgId) ] ]; }Incorrect Route Parameter Access
Make sure you're pulling the right parameter from the route. If your update route is defined like:Route::put('/organizations/{organization}', 'OrganizationController@update');Then
$this->route('organization')will return the entire Organization model instance, so you need to grab itsidproperty. If your route uses a plain{id}parameter, you can just use$this->route('id')directly.Non-Standard Primary Keys
If your organizations table uses a non-default primary key (likeuuidinstead ofid), you need to specify it in theignore()method:Rule::unique('organizations')->ignore($orgUuid, 'uuid')
Why Your Old Controller Approach Worked
In your previous controller code, you were probably doing something like this:
$orgId = $request->route('organization')->id; $validator = Validator::make($request->all(), [ 'name' => [ 'required', Rule::unique('organizations')->ignore($orgId) ] ]);
Here, you directly accessed the route parameter and passed it to the ignore() method—you just need to replicate that logic inside your Form Request's rules() method.
Quick Check
Double-check that:
- You've imported the
Illuminate\Validation\Ruleclass in your Form Request. - The route parameter name in
$this->route('...')matches exactly what's in your routes file. - The ID you're passing to
ignore()is the correct primary key value for the organization you're updating.
That should resolve the unique validation error and let you update your organization without issues!
内容的提问来源于stack exchange,提问作者Latheesan

