php Laravel 有没有办法向请求数组添加值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37107903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel is there a way to add values to a request array
提问by Vijayanand Premnath
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.
我在 Laravel 中遇到了一种情况,即在调用 Eloquent 函数之前调用带有 Request 参数的 store() 或 update() 方法向请求添加一些附加值时,有什么办法可以做到这一点。
function store(Request $request)
{
// some additional logic or checking
User::create($request->all());
}
回答by Alexey Mezenin
Usually, you do not want to add anything to a Request object, it's better to use collection and put()
helper:
通常,您不想向 Request 对象添加任何内容,最好使用 collection 和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->request->add(['variable' => 'value']); //add request
回答by mushood badulla
Referring to Alexey Mezenin
answer:
参考Alexey Mezenin
答案:
While using his answer, I had to add something directly to the Request Object and used:
在使用他的回答时,我不得不直接向请求对象添加一些内容并使用:
$request->request->add(['variable', 'value']);
Using this it adds two variables :
使用它添加两个变量:
$request[0] = 'variable', $request[1] = 'value'
If you are a newbie like me and you needed an associate array the correct way to do is
如果您是像我这样的新手并且需要一个关联数组,那么正确的做法是
$request->request->add(['variable' => 'value']);
Hope I saved your some time
希望我为你节省了一些时间
PS: Thank you @Alexey
, you really helped me out with your answer
PS:谢谢@Alexey
,你的回答真的帮了我大忙
回答by Ferhat KO?ER
I tried $request->merge($array)
function in Laravel 5.2 and it is working perfectly.
我$request->merge($array)
在 Laravel 5.2 中尝试过函数,它运行良好。
Example:
例子:
$request->merge(["key"=>"value"]);
回答by Wiicho Orozco
In laravel 5.6 we can pass parameters between Middlewares for example:
在 laravel 5.6 中,我们可以在中间件之间传递参数,例如:
FirstMiddleware
第一中间件
public function handle($request, Closure $next, ...$params)
{
//some code
return $next($request->merge(['key' => 'value']));
}
SecondMiddleware
第二中间件
public function handle($request, Closure $next, ...$params)
{
//some code
dd($request->all());
}
回答by cosminpanait
You can also use below code
您也可以使用以下代码
$request->request->set(key, value).
$request->request->set(key, value).
Fits better for me.
更适合我。
回答by f_i
enough said on this subject but i couldn't resist to add my own answer. I believe the simplest approach is
关于这个问题已经说得够多了,但我忍不住要添加我自己的答案。我相信最简单的方法是
request()->merge([ 'foo' => 'bar' ]);
回答by Rahul Gupta
To add a new parameter for ex: newParam
to the current Request
Object, you can do:
要将 ex: 的新参数添加newParam
到当前Request
对象,您可以执行以下操作:
$newParam = "paramvalue";
$request->request->add(['newParam' => $newParam]);
After adding the new parameter, you would be able to see this newly added parameter to the Request object by:
添加新参数后,您将能够通过以下方式在请求对象中看到这个新添加的参数:
dd($request);//prints the contents of the Request object
回答by Kreshnik
You can access directly the request array with $request['key'] = 'value'
;
您可以直接访问请求数组$request['key'] = 'value'
;
回答by Abdulrehman Sheikh
The best one I have used and researched on it is $request->merge([])(Check My Piece of Code):
我使用和研究过的最好的一个是$request->merge([])(检查我的代码片段):
public function index(Request $request) {
not_permissions_redirect(have_premission(2));
$filters = (!empty($request->all())) ? true : false;
$request->merge(['type' => 'admin']);
$users = $this->service->getAllUsers($request->all());
$roles = $this->roles->getAllAdminRoles();
return view('users.list', compact(['users', 'roles', 'filters']));
}
Check line # 3 inside the index function.
检查索引函数内的第 3 行。
回答by gecko
Based on my observations:
根据我的观察:
$request->request->add(['variable' => 'value']);
will (mostly) work in POST, PUT & DELETEmethods, because there is value(s) passed, one of those is _token
. Like example below.
$request->request->add(['variable' => 'value']);
将(主要)在POST、PUT 和 DELETE方法中工作,因为传递了值,其中之一是_token
. 像下面的例子。
<form action="{{ route('process', $id) }}" method="POST">
@csrf
</form>
public function process(Request $request, $id){
$request->request->add(['id' => $id]);
}
But [below code] won't work because there is no value(s) passed, it doesn't really add.
但是 [下面的代码] 将不起作用,因为没有传递值,它并没有真正添加。
<a href='{{ route('process', $id) }}'>PROCESS</a>
public function process(Request $request, $id){
$request->request->add(['id' => $id]);
}
使用时 GET您可以声明GET方法Request直接请求并为其分配值。像下面这样:
public function process($id){
$request = new Request(['id' => $id]);
}
Or you can use merge
. This is better actually than $request->request->add(['variable' => 'value']);
because can initialize, and addrequest values that will work for all methods (GET, POST, PUT, DELETE)
或者你可以使用merge
. 这实际上比$request->request->add(['variable' => 'value']);
因为可以初始化和添加适用于所有方法(GET、POST、PUT、DELETE)的请求值更好
public function process(Request $request, $id){
$request->merge(['id' => $id]);
}
Tag:laravel5.8.11
标签:laravel5.8.11