Reputation: 141
I have this error when i try to upload my file : Call to a member function store() on null
I readed documentation many times and i checked SO but i cant understand, could you help me please ?
there is my bootstrap :
<label for="Ticketimg" class="col-md-4 col-form-label text-md-center">{{ __('Ticketimg') }}</label>
<div class="col-md-6">
<input id="Ticketimg" type="file" class="custom-file-label @error('Ticketimg') is-invalid @enderror" name="Ticketimg" enctype="multipart/form-data" required autocomplete="Ticketimg" autofocus> @error('Ticketimg')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span> @enderror
and there is the controller :
public function DeclareFrais($idMission, Request $request)
{
// $image = $request->input('Ticketimg');
$imageCarbu = $request->input('ticketcarbu');
$imageManger = $request->input('ticketmanger');
$prixhotel = $request->input('PrixHotel');
$prixcarbu = $request->input('PrixCarbu');
$prixManger = $request->input('PrixManger');
date_default_timezone_set('Europe/Paris');
$pdate = date('Y-m-d H:i');
$path = $request->file('Ticketimg')->store('public');
// Storage::disk('local')->put('nique', $fichierFinal);
// $test->save();
DB::insert('exec Dfrais ?, ?, ?, ?, ?, ?, ?, ?', array($idMission, $prixhotel, $prixcarbu, $prixManger, $pdate, $path, $imageCarbu, $imageManger));
return redirect()->action('HomeController@show')->with('succes', 'Frais déclarés');
}
i think its something stupid but i cant found it.
NB : my urls $imageCarbu and $imageManger are well stored on my DB, also the html looks ok.
Thnaks for help :)
Upvotes: 1
Views: 103
Reputation: 3030
The mostly reason this is happening (it is hard to tell since you didn't add your form tag) is because, as others have stated, you probably don't have enctype="multipart/form-data"
as part of your FORM
tag.
Why does this matter? The encoding type (enctype) tells the recipient of the form (server side) that that incoming data (which is just base64 encoded text) has separation boundaries with accompanying MIME types between the form data (other submitted inputs) and the file data (contents of the file). Without it, all the data is mixed together.
In your case, it is submitted as regular data, and so Laravel sees the form field Ticketimg as just a regular string of nonsense characters. This means that while Laravel says it is not null (it can see the string), but it doesn't treat it as a File object in the request. From there, it is trying to call store on a string, not a File.
Hope this explains it.
Upvotes: 2