add Or Inject values to a request array or object

591 0 0 0

Last Updated : 2024-04-19 06:48:20

In some cases you might want to inject extra values to the request object that already is holding a form data for example, to do so there are numerous ways, here are some:

$request->request->add(['variable' => 'value']); //add request

function store(Request $request) 
{
// some additional logic or checking
User::create(array_merge($request->all(), ['index' => 'value']));
}

//OR

User::create($request->all() + ['index' => 'value']);

Mohammed Anwar

Mohammed Anwar

Experienced technical lead PHP, MySQL and Laravel Developer for 15+ years, with proven ability to develop and create high-quality and optimized web applications. Great ability to build and optimize database design, schema and queries. Versed programing trainer and instructor delivering web courses and helping others to get into the field in a timely manner. Fast and eager learner for new technologies .