Access model binding in FormRequests

When using FormRequests, you can always access the binding model by simply using the following expression $𝘁𝗵𝗶𝘀->{𝗿𝗼𝘂𝘁𝗲-𝗯𝗶𝗻𝗱𝗶𝗻𝗴-𝘃𝗮𝗿𝗶𝗮𝗯𝗹𝗲}

Here's an example.

class CommunityController extends Controller
{
// ...
public function update(CommunityUpdateRequest $request, Community $community)
{
$community->update($request->validated());
 
return to_route('communities.index')->withMessage('Community updated successfully.');
}
// ...
}
 
class CommunityUpdateRequest extends FormRequest
{
// ...
public function rules()
{
return [
'name' => ['required', Rule::unique('communities', 'name')->ignore($this->community)],
'description' => ['required', 'min:5'],
];
}
// ...
}

Tip given by @bhaidar

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 59 courses (1056 lessons, total 44 h 09 min)
  • 78 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials