Black Friday: coupon FRIDAY24 for 40% off Yearly/Lifetime membership! Read more here

How to "artificially" add values to Request array

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!

No comments or questions yet...

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 67 courses (1172 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