In Laravel is there a way to add values to a request array? Ask Question

In Laravel is there a way to add values to a request array? Ask Question

I come across a situation in Laravel while calling a store() or update() method with Request parameter to add some additional value to the request before calling Eloquent functions is there any way for that.

function store(Request $request) 
{
  // some additional logic or checking
  User::create($request->all());
}

ベストアンサー1

Usually, you do not want to add anything to a Request object, it's better to use collection and put() helper:

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

Or you could union arrays:

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

But, if you really want to add something to a Request object, do this:

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

おすすめ記事