简体   繁体   English

访问FormRequest laravel中的url参数

[英]access to url parameter in FormRequest laravel

To update a user I used a FormRequest named UserByAdminFormRequest like this : 为了更新用户,我使用了一个名为UserByAdminFormRequest如下所示:

public function rules()
    {
        switch ($this->method()) {
            case 'GET':
            case 'DELETE': {
                //some Rules
            }
            case 'POST': {
                //some Rules
            }
            case 'PUT':
                $user_id = $this->get('user_id');

                dd($this);

                return [
                    'name'        => 'required',
                    'email'       => 'email|unique:users,email,' . $user_id . ',user_id',
                    'password'    => 'min:4',
                    're-password' => 'required_unless:password,' . NULL . '|same:password',
                ];


            case 'PATCH': {
                //some Rules
            }
            default:
                break;
        }

As you can see email field should be unique but to ignore current user in this case, I need to her user_id value. 如您所见, email字段应该是唯一的,但是在这种情况下要忽略当前用户,我需要她的user_id值。

For that I used $this->get('user_id') but it return null value always. 为此,我使用了$this->get('user_id')但它始终返回null值。

I used that FormRequest like this in my controller : 我在控制器中使用了这样的FormRequest:

public function update(UserByAdminFormRequest $request, \App\User $user)
    {
        //
    }

And the url that I called by PUT method is (for example) : 我通过PUT方法调用的网址是(例如):

http://api.zarsystem.dev/v1/dashboard/user/7

What can I do in this case Or is there any alternate ways? 在这种情况下我该怎么办?或者有其他替代方法吗?

instead of 代替

$this->get('user_id') $这个 - >获取( 'USER_ID')

you need to use 你需要使用

$this->user_id $这个 - > USER_ID

to get the value of user id, make sure you have input field or hidden filed with name user_id. 要获取用户ID的值,请确保您已输入名称为user_id的输入字段或隐藏文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM