Laravel $request->file() returns null
You should add to the form tag enctype="multipart/form-data"
For example:
<form method="POST" action="{{route('back.post.new')}}" enctype="multipart/form-data">
.............
</form>
Adding it you can use your custom Request.
I hope this can you help!
Noticed that the $request
object I was receiving in the Controller method was an Instance of JsonRequest
, which is a custom class (empty for now) that extends Illuminate\Http\Request
.
And it's implemented as:
<?php
namespace App\Http\Requests;
use Illuminate\Http\Request;
class JsonRequest extends Request {
}
But if I change:
// from
use App\Http\Requests\JsonRequest;
public function add_background_image (JsonRequest $request) {
dd($request->file('files'))
// NULL
}
// to
use Illuminate\Http\Request;
public function add_background_image (Request $request) {
dd($request->file('files'))
// UploadedFile {#266
// -test: false
// -originalName: "item-keymoment.png"
// -mimeType: "image/png"
// -size: 978274
// -error: 0
// ...
// }
}
I get the desired input file. For now switching the instance of $request
solves my issue
But I don't understand why/how extending
Illuminate\Http\Request
with an empty Class breaks things.
Can someone explain?
My intention with subclassing Illuminate\Http\Request
was to be able to attach methods on $requests
for dealing with exceptions/errors in a unified way for API requests. Such as when in deployment show exception messages, but in production return a fixed message.
Is there different/better/more Laravel way of doing that?
I think will just create a JsonController instead of extending Request.