Add Parameters to Laravel Request Object

I encountered an instance where I was looking to add parameters in the Laravel’s Request Object. You might encounter this when you want to add an additional value to the object before calling the store or update

Let’s say you here is your store method

public function store(Request $request){
    Post::create($request->all());
}

If you want to add parameters to the $request object just before the create method. Here is how you can do it.

public function store(Request $request){
    $valuesToAdd = ['value1' => 23, 'value2' => ['one', 'two]];
    $request->merge($valuesToAdd);
    Post::create($request->all());
}

That’s about it.

Related Articles

Leave a reply:

Your email address will not be published.

Site Footer