Laravel is there a way to add values to a request array

前端 未结 13 2130
闹比i
闹比i 2020-12-07 15:24

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 fu

13条回答
  •  北海茫月
    2020-12-07 15:47

    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
    

提交回复
热议问题