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:
  • 68 courses (1183 lessons, total 43 h 18 min)
  • 90 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent New Courses