Reputation: 3912
I have a Company, where I like to validate the update request. Until now I validated the request inside the update()
action but I like to move this to its own CompanyUpdateRequest
.
In the validation I check of the uniqueness of the tax number but of course I like to allow the same tax number for the very company.
'tax_number' => [
'required',
'string',
Rule::unique('companies')->ignore($company->tax_number),
],
This works as long it is placed inside the action, where I have $company
already:
public function update(Request $request, Company $company)
{
}
My question is now, how I get the company inside the CompanyUpdateRequest
?
I know that I could put the ID of the company inside a hidden field in the form, send it along with the request, pull the company from DB ... but this feels kind of wrong. Does anybody have a better / another approach or idea?
Thanks a lot.
Upvotes: 1
Views: 334
Reputation: 9853
use route()
method. Assume your route parameter name is company
-
$this->route('company');
Note:
parameter
method inside route method needs to exactly same asurl route
parameter. In this case-
Route::post('yourUrl/{company}','SomeController@method');
Upvotes: 1
Reputation: 7489
You can get it with $this->route('paramName');
Rule::unique('companies')->ignore($this->route('company')),
Upvotes: 1
Reputation: 163788
You can pass any data through a form:
<input name="company_id" value="{{ $company->tax_number }}" type="hidden">
Then in the CompanyUpdateRequest
class:
Rule::unique('companies')->ignore($request->company_id),
You can also change this rule to:
'companies' => 'unique:companies,company_id,' . $request->company_id,
Upvotes: 1