I haven't used Livewire, but my guess is that you still need to tell it that the form is multipart to be able to upload files.
enctype="multipart/form-data">
I have no idea what's wrong with my code, I have followed every step of Caleb's tutorial on uploading. (I'm a beginner in livewire)
Here are the livewire components.
<?php
namespace App\Http\Livewire;
use App\Models\Message;
use Livewire\Component;
use Livewire\WithFileUploads;
class AddMessage extends Component
{
use WithFileUploads;
public $file;
public $message = '';
public $channel;
protected $rules = [
'message' => ['required'],
'file' => 'image'
];
public function addMessage()
{
$this->validate();
$filename = $this->file->store('/', 'fileuloads');
Message::create([
'body' => $this->message,
'shop_id' => session()->get('shop_id'),
'channel_id' => $this->channel->id,
'user_id' => auth()->user()->id,
'filename' => $filename,
]);
$this->message = '';
}
public function render()
{
return view('livewire.add-message');
}
}
Here is the form
<form wire:submit.prevent="addMessage" action="" class="max-w-md mx-auto mt-8 mb-0 space-y-4">
@csrf
<div>
<label for="message" class="sr-only">Message</label>
<div class="relative">
<input wire:model="message" type="text" class="w-full p-4 pr-12 text-sm border-gray-200 rounded-lg shadow-sm" placeholder="Enter message" />
</div>
@error('message')
<p class="mt-2 text-sm text-red-600">{{ $message }}</p>
@enderror
</div>
<div class="flex items-center justify-between">
<div><input wire:model="file" type="file" name="file" id="file"></div>
<button type="submit" class="inline-block px-5 py-3 ml-3 text-sm font-medium text-white bg-blue-500 rounded-lg">
Post now
</button>
</div>
</form>
Actually, I received an error message until I added the validation on the file property. Now I'm getting no feedback whats wrong at all.
Please or to participate in this conversation.