laravel 如何获取从PUT方法传递的数据

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

How to get data passed from PUT method

phplaravellaravel-5laravel-5.3laravel-5.4

提问by Karthik

I am creating an API for Laravel. I use the PUT method for updating data.

我正在为 Laravel 创建一个 API。我使用 PUT 方法来更新数据。

I send data with Postman using the PUT method. In my controller, I got an empty array. How to access the passed data?

我使用 PUT 方法与 Postman 发送数据。在我的控制器中,我得到了一个空数组。如何访问传递的数据?

In my route, I have:

在我的路线中,我有:

Route::put('vehicletypes/{id}','API\VehicletypeController@update');

In my controller:

在我的控制器中:

public function update(Request $request, $id){

print_r($request->all()); exit;

}

enter image description here

在此处输入图片说明

How to get the data passed in PUTmethod?

如何获取PUT方法中传递的数据?

回答by mega6382

You are getting empty response because PHP have some security restrictions against PUT. But Laravel have a workaround for this.
So, to solve this you have to send a POSTrequest from Postman instead, with a POSTparam __methodwith value PUT. And it should work.

您得到的是空响应,因为 PHP 对PUT. 但是 Laravel 有一个解决方法。
因此,要解决此问题,您必须改为POST从 Postman发送请求,并使用值为的POST参数。它应该工作。__methodPUT

回答by Sagar Dave

Laravel cheats because html forms only support GET and POST, but it does understand a real PUT/PATCH request.

Laravel 会作弊,因为 html 表单只支持 GET 和 POST,但它确实理解真正的 PUT/PATCH 请求。

The problem looks like lies in Symfony it can't parse the data if it's multipart/form-data, as an alternative try using x-www-form-urlencoded content disposition.

问题似乎出在 Symfony 中,如果它是 multipart/form-data,则无法解析数据,作为替代尝试使用 x-www-form-urlencoded content disposition

回答by Himanshu Upadhyay

public function putUpdate(Request $request, $id){

print_r($request->all()); exit;

}

And change route too,

也改变路线,

Route::put('vehicletypes/{id}','API\VehicletypeController@putUpdate');

回答by Zahoor Ahmed

Why nobody is giving clear explanation at first put method field of

为什么没有人首先给出明确的解释 put 方法字段

{{method_field('put')}}

as your router uri is that is displayed using command

因为您的路由器 uri 是使用命令显示的

php artisan router:list

of update method is put/patch so first add

更新方法是 put/patch 所以首先添加

{{method_field('put')}}

and field in your form should be the same

和表单中的字段应该相同

<form action="{{route('posts.update',$post->id)}}" method="post">

after adding the csrf_toke form will be working. and final shape would be as below of form.

添加 csrf_toke 表单后将起作用。最终形状如下表所示。

<form action="{{route('posts.update',$post->id)}}" method="post" >
  {{method_field('put')}}
          <input type="hidden" name="_token" value="{{csrf_token()}}">
            <!-- Name input-->
            <div class="form-group">
              <label class="col-md-3 control-label" for="name">Title</label>
              <div class="col-md-9">
                <input id="name" name="title" type="text" value="{{$post->title}}" class="form-control">
              </div>
            </div>          


    <!-- Message body -->
    <div class="form-group">
      <label class="col-md-3 control-label" for="body">
      Body</label><br>
      <div class="col-md-9">
    <textarea class="form-control" id="message" name="body" rows="5">
    {{$post->body}}
    </textarea>
      </div>
    </div>    
    <!-- Form actions -->
    <div class="form-group">
      <div class="col-md-9 text-right col-md-offset-3">
        <button type="submit" class="btn btn-success btn-lg">Update</button>
        <a href="{{Route('posts.index')}}" type="button" class="btn btn-primary btn-lg">Cancel</a>
      </div>
    </div>         
  </form>

回答by jalalkhan121

Checked "x-www-form-urlencoded" instead of "form-data" under body tab in the Postman, the put methood will work as well...

在 Postman 的 body 选项卡下检查了“x-www-form-urlencoded”而不是“form-data”,put 方法也可以工作......

回答by Waqas Ahmed

Following link will resolve the issue to get the request form data plus the images.
If you are working on Laravel, you can easily parse and bind the form params in the \Illuminate\Http\Request Instance

以下链接将解决获取请求表数据和图像的问题。
如果您正在使用 Laravel,您可以轻松解析和绑定 \Illuminate\Http\Request Instance 中的表单参数

Simply just get the ParseInputStream Class from the link below:
Class ParseInputStream.php

只需从下面的链接中获取 ParseInputStream 类:
Class ParseInputStream.php

Create another Validation Rule Class in Laravel optional but not required,

在 Laravel 中创建另一个验证规则类可选但不是必需的,

class NoteUpdateRequest extends Request {

public function all($keys = null)
{
    if(strtolower($this->getMethod())=='put' && preg_match('/multipart\/form-data/', $this->headers->get('Content-Type')) or
        preg_match('/multipart\/form-data/', $this->headers->get('content-type')))
    {
        $result = [];
        new ParseInputStream($result);
        $result = array_merge($result, $this->route()->parameters());
        $this->request->add($result);
    }

    return parent::all($keys);
}

public function rules()
{
    dd($this->all());
    return [
        'noteId'        => 'required|integer|exists:notes,id',
        'title'         => 'required|string|max:200',
        'note'          => 'required|string|min:10|max:2000'
    ];
 }
}

enter image description here

在此处输入图片说明

I hope it resolved what you want to achieve, using PUT or PATCH method along with Form Params

我希望它解决了您想要实现的目标,使用 PUT 或 PATCH 方法以及 Form Params