Laravel:使用路由/模型绑定时在表单请求中访问模型实例

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

Laravel: Access Model instance in Form Request when using Route/Model binding

phplaravellaravel-5

提问by Jonathon

I have some route/model binding set up in my project for one of my models, and that works just fine. I'm able to use my binding in my route path and accept an instance of my model as a parameter to the relevant method in my controller.

我在我的项目中为我的一个模型设置了一些路由/模型绑定,并且效果很好。我可以在我的路由路径中使用我的绑定并接受我的模型实例作为我控制器中相关方法的参数。

Now I'm trying to do some work with this model, so I have created a method in my controller that accepts a Form Request so I can carry out some validation.

现在我正在尝试使用这个模型做一些工作,所以我在我的控制器中创建了一个接受表单请求的方法,以便我可以执行一些验证。

public function edit(EditBrandRequest $request, Brand $brand)
{
    // ...

Each different instance of my model can be validated differently, so I need to be able to use an instance of the model in order to build a custom set of validation rules.

我的模型的每个不同实例都可以进行不同的验证,因此我需要能够使用模型的实例来构建一组自定义的验证规则。

Is there a way of getting the instance of the model, that is injected into the controller from the Form Request?

有没有办法获取模型的实例,从表单请求注入控制器?

I have tried type-hinting the model instance in the Form Request's constructor

我已经尝试在表单请求的构造函数中对模型实例进行类型提示

class EditBrandRequest extends Request
{
    public function __construct(Brand $brand)
    {
        dd($brand);
    }

I have also tried type-hinting the model instance in the Form Request's rules()method.

我还尝试在 Form Request 的rules()方法中对模型实例进行类型提示。

class EditBrandRequest extends Request
{
    // ...

    public function rules(Brand $brand)
    {
        dd($brand);

In both instances I am provided an empty/new instance of the model, rather than the instance I am expecting.

在这两种情况下,我都提供了模型的空/新实例,而不是我期望的实例。

Of course, I could always get around this by not bothering with Form Requests and just generate the rules in the controller and validate manually - but I would rather do it the Laravel wayif it's possible.

当然,我总是可以通过不打扰表单请求并只在控制器中生成规则并手动验证来解决这个问题 - 但如果可能的话,我宁愿用Laravel 方式来做。

Thanks

谢谢

回答by Rifki

You can simply access it using the binding key, so for example if you bind Brandmodel: $router->model('brand', '\App\Brand')you can get instance of your model with $this->brand. Here is validation rules example:

您可以使用绑定键简单地访问它,例如,如果您绑定Brand模型:$router->model('brand', '\App\Brand')您可以使用$this->brand. 这是验证规则示例:

'slug' => 'required|unique:brand,slug,' . $this->brand->id,

EDIT

编辑

Sometimes you might have an input name that uses the same name as the binding key, for example, if you bind Addressmodel as addressthen you have an input field addressit will make Laravel confuse. For this situation you can use route()method.

有时您可能有一个与绑定键使用相同名称的输入名称,例如,如果您将Address模型绑定为address那么您有一个输入字段,address它会使 Laravel 混淆。对于这种情况,您可以使用route()方法。

'address' => 'required|unique:addresses,address,' . $this->route('address')->id,