How to add extra values in Laravel Request

How to add extra values in Laravel Request

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
	]);

Another simple way is to use merge method.

$request->merge(['extraRequestKey' => extraRequestValue);

Hope it will be helpful for you. Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *