Add custom value to Request in Laravel

Jurgen Bosch
Sep 20, 2018 · 1 min read

I am currently working on a project where I combine VueJS with Laravel. I love this stack as it is flexible, fast and reliable and encountered a situation where I needed to add a value to the request.

In one of my Vue posts (I use Axios for the requests to the server) I use the same component to store a question in my database. With a property I parse the type (page or exam) of the question to determine the post route.

The payload of my post is always the same where I inject a parent ID which in my database can be a exam_id or null. To keep things in my component consistent I always pass the value as parent_id.

In my controller function where I store the exam I need to change the parent_id value to exam_id since that’s the name of the column in the database.

In order to do this I use the following code to add a key to the Illuminate request;

public function store( Request $request, Exam $exam )
$request->request->add(['exam_id' => $request->parent_id]);
$question = $this->storeQuestion($request);

return response()->json($question);

In the example above I add the exam_id to the Request with the value of the parent_id as I received it in the post request.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store