在 Laravel 中保存表单数据的正确方法是什么(使用注入模型)?

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

What is the proper way to save form data in Laravel (using an injected model)?

phplaravellaravel-4dependency-injection

提问by hdwebpros

I'm trying to setup a simple form to save, but want to be sure we are using best practices, like DI.

我正在尝试设置一个简单的表单来保存,但要确保我们使用的是最佳实践,例如 DI。

In the controller file, I have

在控制器文件中,我有

public function store()
{
    //get form data
    $data = Input::all();

    $newclient = new Client($data);
    $newclient->save();
    return Redirect::route('clients.index');
}

But that really isn't dependency injection. (right?) I injected the model like this

但这真的不是依赖注入。(对吗?)我是这样注入模型的

public function __construct(\Client $clientmodel)
{
    $this->clientmodel=$clientmodel;
}

How would I save the form data on the store function properly, using dependency injection?

如何使用依赖注入正确保存存储功能上的表单数据?

回答by lukasgeiter

Looking at the __constructorin Illuminate\Database\Eloquent\Modelyou can see it uses fill()to assign the passed in values.

查看__constructorinIlluminate\Database\Eloquent\Model您可以看到它用于fill()分配传入的值。

public function __construct(array $attributes = array())
{
    $this->bootIfNotBooted();
    $this->syncOriginal();
    $this->fill($attributes);
}

So you can just use fill()to do the same after the class has been instantiated:

所以你可以fill()在类被实例化后使用它来做同样的事情:

$this->clientmodel->fill($data)
$this->clientmodel->save();

Or if you want to just save it anyways, you can use create():

或者,如果您只想保存它,您可以使用create()

$this->clientmodel->create($data);

回答by nCrazed

If you are always creating a new object then you can use the Eloquent Model's createmethod like so:

如果你总是创建一个新对象,那么你可以create像这样使用 Eloquent 模型的方法:

public function store()
{
    //get form data
    $data = Input::all();

    $this->clientmodel->create($data);

    return Redirect::route('clients.index');
}

If it's possible that sometimes this route will be handling updates of an existing Clientrecord then you should take a look at the firstOrCreateand firstOrNewmethods.

如果有时这条路线可能会处理现有Client记录的更新,那么您应该查看firstOrCreatefirstOrNew方法。

回答by Ray

What you did is just a good practice. That'd save you a lot of codes. However, there are more remainders.

你所做的只是一个很好的做法。那会为你节省很多代码。然而,还有更多的余数。

  1. Always iterate over each input to check which input is absent. Well, in your case, it's a simple form. But if it's an API, clients send various inputs and you need to know what to expect. Sometimes when an input is absent, it can cause serious problems like assigning the value of an undefined input to a model.
  2. Always check whether an input is valid. Make sure every input is up to your standard. You can make various validations to make it happen.
  1. 始终迭代每个输入以检查哪个输入不存在。好吧,就您而言,这是一个简单的形式。但如果是 API,客户端会发送各种输入,您需要知道会发生什么。有时,当缺少输入时,可能会导致严重的问题,例如将未定义输入的值分配给模型。
  2. 始终检查输入是否有效。确保每个输入都符合您的标准。您可以进行各种验证以实现它。

Other than the above two points, you've walked pretty far on the right track.

除了以上两点,你已经在正确的轨道上走了很远。