php Laravel 5.5 request()->all() 没有得到所有的输入

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

Laravel 5.5 request()->all() doesn't get all the inputs

phplaravel

提问by Leo

I am using Laravel 5.5. I am trying to create a post, but when I request for all() data to die dump from request instance I don't get all the fields printed out.

我正在使用 Laravel 5.5。我正在尝试创建一个帖子,但是当我请求 all() 数据从请求实例中消失时,我没有打印出所有字段。

Here is my code to create the post:

这是我创建帖子的代码:

   /**

    * Persist new post.

    */
public function store()
{
    $this->validate(request(),[

        'title'=>'required' 
    ]);

        dd(request()->all());

        $path = CreatePhotoThumbnail(request()->file('photo'));

        auth()->user()->addPost(new Posts( [
        'title'=>request('title'),
        'body'=>request('body'),
        'photo'=> $path

    ]));
}

All i get in print_Ris the title only:

我得到的print_R只是标题:

Array ( [_token] => MhOTEGkR1oDMc50q0FiJmI8JCAeuCRrFCfRHcKkq  [title] => test )

Edited:

编辑:

The form:

表格:

  <!-- Main (left side) -->

  <section style="margin-top:20px;">

    <div class="row">
        <div class="col-sm-12">

          <!-- post -->
          <article class="blog-post">


          <div class="post-entry">

          <h2>Create a Blog Post</h2>

          <p>Be as specific as u can:</p>


          <form name="" action="/posts/create" method="post" class="comment-form" enctype="multipart/form-data">
          {{csrf_field()}}
          <div style="display: none;">
          <input type="hidden" name="_wpcf7" value="79" />
          <input type="hidden" name="_wpcf7_version" value="4.1" />
          <input type="hidden" name="_wpcf7_locale" value="en_US" />
          <input type="hidden" name="_wpcf7_unit_tag" value="wpcf7-f79-p64-o1" />
          <input type="hidden" name="_wpnonce" value="ebcdc94d2e" />
          </div>
          <div class="row">


          <div class="col-md-12">
            <label for="title">Post Title</label>
            <input id="title" type="text" placeholder="Post Title" name="title">
          </div>


          <div class="col-md-12">
            <label for="body">Body:</label>
            <textarea name="body" id="body" placeholder="Post body" rows="10"></textarea>
          </div>

          <div class="col-md-12" id="drop">
              <label for="photo">Upload a post picture</label>
              <input type="file" id="photo"  name="photo" >
          </div>

          <div class="col-md-12"><input type="submit" value="Create Post" class="submit-button" /></div>
          </div>
          </form>


          </div>
          <div class="col-md-12" style="padding: 0px; margin:0px;">
              @include('layouts.errors')
          </div>
          </article>
          <!-- contact end -->

        </div><!-- end col-md-12 -->
    </div><!-- end row -->

   </section>
   <!-- END Main (left side) -->

采纳答案by Ben Swinburne

Laravel 5.5 changed the validate()method to return validated fields so that you could easily pass valid data to model creation without using request->only()for example.

Laravel 5.5 更改了validate()返回已验证字段的方法,以便您可以轻松地将有效数据传递给模型创建,而无需使用request->only()例如。

It is possible that it is unintentionally modifying $request->all()and as such you're not getting the results you intended.

它可能是无意中修改的$request->all(),因此您没有得到您想要的结果。

If you want to get everything, try adding your fields to your validator, even if you don't want to validate them, i.e. without actual rules.

如果您想获得所有内容,请尝试将您的字段添加到您的验证器,即使您不想验证它们,即没有实际规则。

For example

例如

$validData = $this->validate(request(),[
    'title'=>'required',
    'body' => ''
]);

$validData should contain your fields.

$validData 应包含您的字段。

You might also try

你也可以试试

$this->validate(request()->all() ...

Which might then not modify the request object meaning $request->all()might work as expected

然后可能不会修改请求对象的含义$request->all()可能会按预期工作