Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

laravel

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) -->
like image 717
Leo Avatar asked Jul 07 '17 19:07

Leo


People also ask

How do you get all input of get in laravel?

All that is needed is the line use Illuminate\Support\Facades\Request; at the top of the file.

What is request -> input () in laravel?

input() is a method of the Laravel Request class that is extending Symfony Request class, and it supports dot notation to access nested data (like $name = $request->input('products.0.name') ).

How can you retrieve the full URL for the incoming request?

Retrieving the Request URI The “path” method is used to retrieve the requested URI. The is method is used to retrieve the requested URI which matches the particular pattern specified in the argument of the method. To get the full URL, we can use the url method.

How does request work in laravel?

Laravel itself creates an instance of the application, is the initial/first step. Next step will occur on the Kernel part of the application. The incoming request is sent to either the HTTP kernel or the console kernel, depending on the type of request that is entering the application .


1 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

like image 194
Ben Swinburne Avatar answered Oct 01 '22 10:10

Ben Swinburne