Laravel 在请求中检索绑定模型

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

Laravel retrieve binded model in Request

phplaravellaravel-5

提问by Phil Cross

Is there any easy way of retrieving the route binded model within a Request?

是否有任何简单的方法可以在请求中检索路由绑定模型?

I want to update a model, but before I do, I want to perform some permissions checks using the Requests authorize()method. But I only want the owner of the model to be able to update it.

我想更新模型,但在此之前,我想使用 Requestsauthorize()方法执行一些权限检查。但我只希望模型的所有者能够更新它。

In the controller, I would simply do something like this:

在控制器中,我会简单地做这样的事情:

public function update(Request $request, Booking $booking)
{
    if($booking->owner->user_id === Auth::user()->user_id)
    {
       // Continue to update
    }
}

But I'm looking to do this within the Request, rather than within the controller. If I do:

但我希望在请求中执行此操作,而不是在控制器中执行此操作。如果我做:

dd(Illuminate\Http\Request::all());

It only gives me the scalar form properties (such as _methodand so on, but not the model).

它只给我标量形式的属性(例如_method等等,而不是模型)。

Question

If I bind a model to a route, how can I retrieve that model from within a Request?

如果我将模型绑定到路由,如何从请求中检索该模型?

Many thanks in advance.

提前谢谢了。

回答by Martin Bean

Absolutely! It's an approach I even use myself.

绝对地!这是我什至自己使用的方法。

You can get the current route in the request, and then any parameters, like so:

您可以在请求中获取当前路由,然后获取任何参数,如下所示:

class UpdateRequest extends Request
{
    public function authorize()
    {
        $booking = $this->route('booking');

        return $booking->owner->user_id == $this->user()->getKey();
    }
}

Unlike smartman's (now deleted) answer, this doesn't incur another find query if you have already retrieved the model via route–model binding.

smartman的(现已删除)答案不同,如果您已经通过路由-模型绑定检索了模型,这不会引发另一个查找查询。

回答by godbout

Once you did your explicit binding (https://laravel.com/docs/5.5/routing#route-model-binding) you actually can get your model directly with $this.

一旦您进行了显式绑定(https://laravel.com/docs/5.5/routing#route-model-binding),您实际上可以直接使用 $this 获取您的模型。

class UpdateRequest extends Request
{
    public function authorize()
    {
        return $this->booking->owner->user_id == $this->booking->user()->id;
    }
}

Even cleaner!

更干净!

回答by Andrew Ellis

To add on to Martin Bean's answer, you can access the bound instance using just route($param):

要添加Martin Bean的答案,您可以仅使用以下命令访问绑定实例route($param)

class UpdateRequest extends Request
{
    public function authorize()
    {
        $booking = $this->route('booking');

        return $booking->owner->user_id == $this->user()->id;
    }
}

Note:This works in Laravel 5.1. I have not tested this on older versions.

注意:这适用于 Laravel 5.1。我没有在旧版本上测试过这个。