Support the ongoing development of Laravel.io →
Requests Forms Validation

If we are doing authorization in the FormRequest is there a way to pass the model to the controller this to prevent performing the same query twice?

public function authorize()
{
	$post = Post::where('id', $this->id)->where('user_id', Auth::id())->get()
	if($post->count()>0){
		// Somehow pass $post to controller
		return true;
	}
	return false;
}

Or should i just cache the model and return it on the controller?

Last updated 3 years ago.
0
Solution

You can set a public variable on the Request instance, something like this:

public function authorize()
{
    $post = Post::where('id', $this->id)->where('user_id', Auth::id())->get()
    if($post->count()>0){
        $this->post = $post;
        return true;
    }
    return false;
}

and then in controller action, for instance:

public function someAction(FormRequestChild $request)
{
      var_dump($request->post);
}

Hope this helps!

Last updated 3 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

Rockroxx rockroxx Joined 13 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.