deathmetalgore's avatar

Laravel Request $request->file() is always blank

I have a form that has a field to upload multiple files. So far so good. So when I select a few files and hit the submit button, the $request->file() is always empty no matter what. The name of my field is images_json.

I suspected the route being PATCH or PUT could cause this (considering I'm uploading images on create and update), I made a separate POST route to handle the uploading with its own controller action and had a 3rd party jQuery plugin handle the upload (blueimp jQuery File Upload plugin), and guess... I'm receiving an empty response yet again!

And yes I have 'files' => true in my form tag and it spits the multiform attribute very nicely.

This is what I have in my view currently (normal file upload that relies on the submit button of the form):

<div class="form-group form-md-line-input">
    {!! Form::label('images_json', 'Bilder hochladen', ['class' => 'col-md-2 control-label']) !!}
    <div class="col-md-10">
        <div class="fileinput fileinput-new" data-provides="fileinput">
            <span class="btn green-seagreen btn-file">
                <i class="glyphicon glyphicon-plus"></i>
                <span class="fileinput-new">Bilder auswählen...</span>
                <span class="fileinput-exists">Mehr auswählen</span>
                {!! Form::file('images_json[]', ['multiple']) !!}
            </span>
            <span class="fileinput-preview" style="max-height: 32px;"></span>
              <a href="javascript:;" class="close fileinput-exists" data-dismiss="fileinput"></a>
        </div>
    </div>
</div>

And this is the action:

public function update(CreateEntryRequest $request, $id)
{
    return $request->file('images_json');
}

And this would be a response:

[{},{},{}]

(Yes, I selected three pictures)

No matter what, it always comes back as blank. I have been stuck in this problem for a couple of days now, postponing it thinking it's a simple issue. But this is now something I am unable to figure out.

Help would be appreciated.

0 likes
13 replies
bobbybouwmann's avatar

Your form needs to know it's sending files as well, add the 'files' => true parameter

Form::open(['method' => 'post', 'route' => 'post.route', 'files' => true])
2 likes
bobbybouwmann's avatar

@bestmomo Oow wow, I totally missed that...

Well for your problem what does this return for you

$request->hasFile('images_json')
deathmetalgore's avatar

Hmm, I fixed this problem by using $_FILES, but I don't really understand why that's the case. Anyone able to explain this to me?

deathmetalgore's avatar

@sid405 but $request->files() wont return anything, so hasFile will obviously return false. There is nothing more to this. The $_FILES global is really something I don't want to do, but there is nothing else I can think of. Hence my question here.

1 like
deathmetalgore's avatar

@sid405 There we go... It has everything beside images_json:

array:82 [▼
  "_method" => "PATCH"
  "_token" => "Kdo7KS7pc3wfVHFPQcEs4KiPEZNnvFc3sdzmLjyj"
  "supplier_id" => "19"
  "customer_id" => "51"
  "contact" => "Shana Emmerich III"
  "commission" => "EMBRjBaACCjztEl"
  "entrance_date" => "05/09/1999"
  "cost_estimate" => "1"
  "status" => "1"
  "type" => "4"
  "watch_id" => "12"
  "reference" => "Norma Daniel"
  "serial_number" => "4899639"
  "delivery_date" => "08/02/2013"
  "cost_estimate_id" => "48"
  "kv_pos1_1_text" => "Possimus et et est totam voluptatum et. Rerum doloribus ipsam adipisci numquam adipisci quo. Sunt tempore distinctio esse non est."
  "kv_pos1_1_price" => "47.00"
  "kv_pos1_1_price_brutto" => "50.00"
  "kv_pos1_1_uvp" => "242.00"
  "kv_pos1_1_info" => "Libero nihil sequi ducimus explicabo maxime itaque. Sit quod iusto velit sit dolore laborum rerum. Occaecati vero explicabo deserunt. Ut qui aperiam voluptatem consequuntur dolorum dolorum dolor omnis."
  "kv_pos1_2_text" => "Nobis doloremque voluptatem deleniti iure ut neque. Reprehenderit sit sit cumque nemo quibusdam. Quasi aspernatur ipsa nesciunt aspernatur saepe. Et dignissimos debitis optio est enim dolore voluptas."
  "kv_pos1_2_price" => "425.00"
  "kv_pos1_2_price_brutto" => "479.00"
  "kv_pos1_2_uvp" => "195.00"
  "kv_pos1_3_text" => "Magni qui eveniet ut voluptas temporibus non provident sequi. Iusto veritatis ut et et. Labore nihil fugit sint eos."
  "kv_pos1_3_price" => "427.00"
  "kv_pos1_3_price_brutto" => "138.00"
  "kv_pos1_3_uvp" => "360.00"
  "kv_pos1_4_text" => "Iste perferendis quo corrupti est. Temporibus culpa et facere mollitia eius facere. Aspernatur fuga facilis eum sint fugiat sit quia. Quia eligendi quo autem accusantium minus minus."
  "kv_pos1_4_price" => "260.00"
  "kv_pos1_4_price_brutto" => "313.00"
  "kv_pos1_4_uvp" => "366.00"
  "kv_pos1_5_text" => "Tempora minima rerum dolore ex inventore aliquam iusto. Quo fuga ut dolores repellat consequatur non. Magni quis minima quia quod voluptas ut quia."
  "kv_pos1_5_price" => "61.00"
  "kv_pos1_5_price_brutto" => "481.00"
  "kv_pos1_5_uvp" => "141.00"
  "kv_pos1_6_text" => "Illum iusto molestiae consequuntur neque neque sunt. Consequuntur omnis officiis in accusantium sunt minus atque. Non voluptas officiis molestiae voluptas ducimus ut."
  "kv_pos1_6_price" => "237.00"
  "kv_pos1_6_price_brutto" => "102.00"
  "kv_pos1_6_uvp" => "205.00"
  "kv_pos1_7_text" => "Et itaque a et veritatis dolores error. Accusamus perspiciatis blanditiis enim sed possimus ut ut. Qui reprehenderit illo eos ad ut natus. Eos dolores voluptatem ut qui."
  "kv_pos1_7_price" => "269.00"
  "kv_pos1_7_price_brutto" => "500.00"
  "kv_pos1_7_uvp" => "308.00"
  "remarks" => "Repellat illo officiis non animi. Totam pariatur quisquam molestiae laudantium odit et exercitationem. Distinctio nesciunt quia exercitationem molestiae. Et doloremque sunt ipsam quasi sit dolorum nam."
  "entrace_inspection" => "Magni doloribus quis ut et. Modi ut aut vel quibusdam enim quo. At dolores libero deleniti sed harum quis deserunt."
  "release_delivery_note" => "0"
  "customer_bill_id" => "42091"
  "kv_pos2_1_text" => "Earum officiis doloremque sit omnis. Voluptas voluptatibus et aliquid at neque aliquid."
  "kv_pos2_1_price" => "290.00"
  "kv_pos2_1_price_brutto" => "339.00"
  "kv_pos2_1_uvp" => "25.00"
  "kv_pos2_1_info" => ""
  "kv_pos2_2_text" => "Numquam ratione velit nostrum non eum sit. Maxime consequatur minus cumque esse. In nam architecto minima pariatur numquam est totam. Quos sunt alias et rerum."
  "kv_pos2_2_price" => "53.00"
  "kv_pos2_2_price_brutto" => "201.00"
  "kv_pos2_2_uvp" => "362.00"
  "kv_pos2_3_text" => "Totam magni numquam id similique dolor. Qui sed magnam placeat eius dolores. Voluptate non dolorem et consequatur impedit laudantium cumque. Et enim voluptas minima id."
  "kv_pos2_3_price" => "25.00"
  "kv_pos2_3_price_brutto" => "321.00"
  "kv_pos2_3_uvp" => "235.00"
  "kv_pos2_4_text" => "Dolores rerum recusandae enim velit explicabo corrupti ratione odit. Ea quibusdam id deleniti autem asperiores ut inventore. Aliquid commodi est voluptas laudantium eaque eaque sunt."
  "kv_pos2_4_price" => "160.00"
  "kv_pos2_4_price_brutto" => "277.00"
  "kv_pos2_4_uvp" => "229.00"
  "kv_pos2_5_text" => "Dolores rerum recusandae enim velit explicabo corrupti ratione odit. Ea quibusdam id deleniti autem asperiores ut inventore. Aliquid commodi est voluptas laudantium eaque eaque sunt."
  "kv_pos2_5_price" => "438.00"
  "kv_pos2_5_price_brutto" => "313.00"
  "kv_pos2_5_uvp" => "26.00"
  "kv_pos2_6_text" => "Nulla repudiandae doloribus dicta modi. Culpa eum veniam voluptatem nemo nisi. Quaerat commodi suscipit enim ea."
  "kv_pos2_6_price" => "144.00"
  "kv_pos2_6_price_brutto" => "225.00"
  "kv_pos2_6_uvp" => "470.00"
  "kv_pos2_7_text" => "Accusamus et et nihil at. Ut cupiditate saepe et velit deleniti sint. Dolores consequatur et doloribus nemo dignissimos fugiat nihil odio. Quia dignissimos est eum sint sequi soluta iure."
  "kv_pos2_7_price" => "367.00"
  "kv_pos2_7_price_brutto" => "290.00"
  "kv_pos2_7_uvp" => "40.00"
  "articles_json" => array:2 [▶]
  "total_sales_cost_netto" => "40.00"
  "gross_profit_netto" => "333.00"
  "gross_profit_brutto" => "119.00"
  "archived" => "0"
]
sid405's avatar

@deathmetalgore when you inspect the html of the generated form, do you see this?

enctype="multipart/form-data
2 likes
sid405's avatar

@deathmetalgore also your final version of the form should be this by now

{!! Form::open(array('url'=>'your-url','method'=>'POST', 'files'=>true)) !!}
<div class="form-group form-md-line-input">
    {!! Form::label('images_json', 'Bilder hochladen', ['class' => 'col-md-2 control-label']) !!}
    <div class="col-md-10">
        <div class="fileinput fileinput-new" data-provides="fileinput">
            <span class="btn green-seagreen btn-file">
                <i class="glyphicon glyphicon-plus"></i>
                <span class="fileinput-new">Bilder auswählen...</span>
                <span class="fileinput-exists">Mehr auswählen</span>
                {!! Form::file('images_json[]', ['multiple' => true]) !!}
            </span>
            <span class="fileinput-preview" style="max-height: 32px;"></span>
              <a href="javascript:;" class="close fileinput-exists" data-dismiss="fileinput"></a>
        </div>
    </div>
</div>
</form>
deathmetalgore's avatar
Level 1

This issue got fixed when I imported this namespace:

use Symfony\Component\HttpFoundation\File\UploadedFile;

Well it could've been something else, but it started working suddenly.

thomas_inckx's avatar

In case this could help someone: I injected the wrong dependency in the controller. Should have been Illuminate\Http\Request in my case.

abedbilani's avatar

i imported use Illuminate\Foundation\Http\FormRequest; instead of Illuminate\Http\Request

Please or to participate in this conversation.