Skip to main content
Tutorial Free

How to "artificially" add values to Request array

May 03, 2016
1 min read
A few times I encountered a situation - a store() or update() method with Request parameter, but I needed to add some additional value to the request before calling Eloquent functions. So how to do that? Apparently, pretty easy. Let's look at the code:
function store(Request $request)
{
  // some additional logic or checking
  User::create($request->all());
}
What if you need to add additional field to the request with value coming from that additional logic block? It would look like this:
function store(Request $request)
{
  // some additional logic or checking
  $plan = 123; // some logic to decide user's plan
  $request->request->add(['plan' => $plan]);
  User::create($request->all());
}
See? The general logic is to add an array of key => value to the $request->request property. Small tip, but hopefully helpful!

Enjoyed This Tutorial?

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

Comments & Discussion

No comments yet…

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.