Add custom value to Request in Laravel
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);
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.