i'm trying to upload some files via form to my db and also in the storage of my project
I did the following code on my homepage :
<x-layout>
@if (session('message'))
<div class="alert alert-success">{{session('message')}}</div>
@endif
<div class="container vh-100">
<div class="row h-100 w-100 align-items-center">
<div class="offset-3 col-6">
<form method="POST" action="{{route('transfer.submit')}}" class="card" enctype="multipart/form-data">
@csrf
<div class="border w-100" id="fileWrapper">
<div class="mb-3 w-100 h-100">
<input type="file" class="form-control w-100 h-100 fileInput" id="fileupload" name="files[]" multiple >
</div>
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Invia file a </label>
<input type="email" class="form-control" id="exampleInputPassword1" name="recipient_mail">
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">La tua mail</label>
<input type="email" class="form-control" id="exampleInputPassword1" name="sender_mail">
</div>
<div class="mb-3">
<input type="text" class="form-control" id="title" name="title">
</div>
<div class="mb-3">
<textarea name="message" cols="50" rows="10"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</x-layout>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
Then i done the following in my model :
protected $fillable = [
'recipient_mail',
'sender_mail',
'title',
'message',
'files[]'
];
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
and the following in my controller :
public function transferSubmit(TransferRequest $request){
$transfer = Transfer::create([
'sender_mail'=>$request->input('sender_mail'),
'recipient_mail'=>$request->input('recipient_mail'),
'title'=>$request->input('title'),
'message'=>$request->input('message'),
'files'=>$request->file('files[]')->store('public/files'),
]);
return redirect(route('home'))->with('message', 'File inviato con successo');
}
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
I havo also created the POST route and completed the migrations but, when i try to submit the form i get the following error :
Error Call to a member function store() on null
After this i tried the dd($request) ro check the data that i was actually passing to the Trasnfer class and i found that it is receiving correctly every data including the array of files.
Is there anybody that can help me to understand why i'm getting that error?
Thank you so much
CodePudding user response:
You want store multiple files. And you will get an array. Then you have to iteratrate over your file array like that.
$files = [];
if($request->hasfile('files[]'))
{
foreach($request->file('files[]') as $file)
{
$files => $file->store('public/files'),
}
}
Important Note:
And don't forget the symlink before working with the Laravel storage.
php artisan storage:link
Updated You iterate first then you have the file array which contains the paths to the images. you can then pass that to your model.
A little note: data coming from a form should always be validated.
public function transferSubmit(TransferRequest $request){
$files = [];
if($request->hasfile('files[]'))
{
foreach($request->file('files[]') as $file)
{
$files => $file->store('public/files'),
}
}
$transfer = Transfer::create([
'sender_mail'=>$request->input('sender_mail'),
'recipient_mail'=>$request->input('recipient_mail'),
'title'=>$request->input('title'),
'message'=>$request->input('message'),
'files'=> $files;
return redirect(route('home'))->with('message', 'File inviato con successo');
}