Skip to main content
Quick Tip

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

Enjoyed This Tip?

Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.