Laravel有一种向请求数组添加值的方法 [英] Laravel is there a way to add values to a request array

查看:674
本文介绍了Laravel有一种向请求数组添加值的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在遇到带有请求参数的store()或update()方法以在请求Eloquent函数之前为请求添加一些附加值的时候,我遇到了Laravel中的情况.

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());
}

推荐答案

通常,您不想向Request对象添加任何内容,最好使用collection和

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

或者您可以联合数组:

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

但是,如果您确实想向Request对象中添加某些内容,请执行以下操作:

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

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

这篇关于Laravel有一种向请求数组添加值的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆