请求-> 除了给我一个数组而不是 laravel 5 中的请求对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36017414/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:24:59  来源:igfitidea点击:

Request->except give me an array instead of a Request object in laravel 5

phplaravellaravel-5httprequest

提问by Juliatzin

$request->except(['param1'])gives me an array, I would like to receive a Requestobject..

$request->except(['param1'])给我一个数组,我想接收一个Request对象..

I'm not sure how to do it...

我不知道该怎么做...

$employee = EmployeeSnap::firstOrNew(['nss' => $request->nss, 'rfc' => $request->rfc]);

        if ($employee->name != null && $employee->name != "") {
            $employee->setData(Input::except(['bankid', 'bankaccount'])); //Input::except(['bankid', 'bankaccount']) gives me array instead of Request
        } else {
            $employee->setData($request);
        }

        $employee->save();
        $id = $employee->employeeid;
        $code = 200;
        $data = 'Success';
        $message = 'Operation successful';

Any idea?

任何的想法?

回答by patricus

Option 1

选项1

You can attempt to clone the Requestobject and unset the unneeded variables, as mentioned by @AlexeyMezenin. However, this is only a shallow copy, so any objects related to the original request will be the exact same objects related to the new request. This may or may not cause you issues.

您可以尝试克隆Request对象并取消设置不需要的变量,如@AlexeyMezenin 所述。然而,这只是一个浅拷贝,所以与原始请求相关的任何对象都将是与新请求相关的完全相同的对象。这可能会也可能不会导致您出现问题。

Option 2

选项 2

You can also attempt to generate a new request and replace the input. Something along the lines of:

您还可以尝试生成新请求并替换输入。类似的东西:

$newRequest = \Illuminate\Http\Request::capture();
$newRequest->replace($request->except(['param1']));

However, depending on how you use this new request, it may cause different issues.

但是,根据您使用此新请求的方式,它可能会导致不同的问题。

Option 3 (best)

选项 3(最佳)

I think the real solution to your problem is to change the setData()method on your EmployeeSnapmodel. Requestobjects really should only be used inside Controllers. The controller should be responsible for interacting with the Requestto get the data needed, and then it should pass that data to your models as needed.

我认为解决您的问题的真正方法是更改模型setData()上的方法EmployeeSnapRequest对象真的应该只在控制器内部使用。控制器应该负责与 交互Request以获取所需的数据,然后它应该根据需要将该数据传递给您的模型。

So, your setDatamethod should look like:

所以,你的setData方法应该是这样的:

public function setData(array $request) {
    // use your $request array
}

And your controller should look like:

你的控制器应该是这样的:

$employee = EmployeeSnap::firstOrNew(['nss' => $request->nss, 'rfc' => $request->rfc]);

if ($employee->name != null && $employee->name != "") {
    $employee->setData($request->except(['bankid', 'bankaccount']));
} else {
    $employee->setData($request->all());
}

// rest of code ...

This helps separate your concerns, keeps your code a little cleaner, and resolves your issue.

这有助于分离您的关注点,使您的代码更简洁,并解决您的问题。

回答by Alexey Mezenin

I think I've understood what you want and why (thanks for updated post with code). I guess you will not be able to do this with standard Laravel methods but you could do something like this:

我想我已经明白你想要什么以及为什么(感谢更新的代码帖子)。我想您将无法使用标准的 Laravel 方法执行此操作,但您可以执行以下操作:

$requestCopy = clone $request;
unset($requestCopy->bankid);
unset($requestCopy->bankaccount);

So, you're cloning instance of Request class and just remove unsafe properties from it with unset(). And then you could use the $requestCopyin your code. That's the only way to do it that I can imagine.

因此,您正在克隆 Request 类的实例,只需使用unset(). 然后你可以$requestCopy在你的代码中使用。这是我能想象的唯一方法。

This code is just an example, you could do something similar which will work for you.

这段代码只是一个例子,你可以做一些对你有用的类似的事情。