liandhas's avatar

Dynamic file uploading in Livewire

I am trying to implement dynamic input file upload in Livewire. I have done this so far, I couldn't get the result. Here is the code,

public function rules() {
    return [
        'page.elements.*.featured_image' => 'nullable|image'
    ]
}

public function store() {
    $this->validate();
    $this->page->save();
    $this->page->elements->each->save();  
}

@foreach($page->elements as $index => $el) {
    <input type="file" wire:model.lazy="page.elements.{{ $index }}.featured_image" />
@endforeach

I tried to store the image in the local disk, Did I miss something? I have tried a couple of ways, unfortunately, nothing works for me.

0 likes
3 replies
liandhas's avatar

@Sinnbeck I have used this method,

foreach($this->page->elements as $index => $el) {
    $this->page->elements->where('id', $el->id)->update([
        'featured_image' => $el->featured_image->store('/', 'images')
    ]);
}

The file is stored in the temporary folder, and it shows an error when submitting the form

call to a member function temporaryurl()
Sinnbeck's avatar

@lianmaymesi try storing it first

foreach($pages->elements as $index => $el) {
    $path = $el->featured_image->store('/', 'images') ;
    $this->page->elements->each->update([
        'featured_image' => $path
    ]);
} 

Please or to participate in this conversation.