Home » Php » php – Laravel 5.5 request()->all() doesn't get all the inputs

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

Posted by: admin July 12, 2020 Leave a comment

Questions:

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.

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_R is the title only:

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) -->
How to&Answers:

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.

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

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.

You might also try

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

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