tonviet712
3 months ago
359
4
Laravel

Is it best practice if I store model data in $request?

Posted 3 months ago by tonviet712

I have several endpoint like:

/{code}/abc

/{code}/def

/{code}/xyz

I want to verify code each request, so I create middleware CheckCode for those request In middleware, my code is something like:

$member = Member::where('code', $request->code)->first();
if (!$member) abort(404);
$this->merge([
	'member' => $member
]);

return $next($request);

And in controller/action for each requests, I just get

$member = $request->member;

So is it best practices for this problem? Do you have any better solution? Thanks in advance

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