RecordExists middleware in controller

Posted 1 year ago by itstrueimryan

Defined a resource route with:

Route::resource('organizations', 'OrganizationController');

Now I'm trying to use the record_exists middleware in the constructor of my resource controller, a la:

public function __construct()
{
    $this->middleware('record_exists:App\Model\Organization', ['only' => ['edit']]);
}

But it results in a 404. If we look at the handle method of RecordExists:

if ($model::find($request->id) === null) {
    abort(404);
}

We can see it checks for the request id, which is null when I define this middleware in the constructor, even though the url is something like /organizations/12/edit. Is there any way to ensure that $request->id is passed, or do I have to define every edit route explicitly in my web.php file (using the middle in the route itself works)?

Route::resource('organizations', 'OrganizationController')->middleware('superadmin');
Route::get('organizations/{id}/edit', '[email protected]')->middleware( 'record_exists:App\Models\Organization');

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.