How to add extra values in Laravel Request

How to add extra values in Laravel Request

Posted on:January 27, 2019 at 10:00 AM

Laravel request is the way to fetch user’s form data. By default, whatever you have written in the form, you are able to receive the data via Laravel Request.

The normal procedure is-

function storeDate(Request $request)
{
	return $request->all();
}

This method will receive all the submitted field data from the web form.

Now, if you need to add extra data into request array, you can add this way.

function storeDate(Request $request)
{
	$authorName = 'Put your value here';

	$myNewData = $request->request->add(['author' => $authorName]);

	return $request->all();
}

Now you are able to access $authorName in the request array.

If you have more than one value you want to add, you can pass another value into the array like this way-

$myNewData = $request->request->add([
		'author' => $authorName
		'author2' => $authorName2
	]);

Hope it will be helpful for you. Thank you.