laravel livewire files always gets uploaded in the last item of an array

Viewed 23

I am trying to build a facebook clone on livewire, and i wanted to assign each comment section its own model so that's why i created an empty array in my class,

public $new_comments = array();

then for each post i tried to assign to its comment section 2 models (one for the comment body and the second one for the picture) based on the post id

<form class="form-group" wire:submit.prevent="test">
   <input name="message" required class="form-control" placeholder="Write a comment..." wire:model="new_comments.{{ $post->id }}.body"></input>
   <label for="comment_image" style="cursor: pointer"><i class="flaticon-photo-camera"></i></label>
   <input type="submit"></input>
   <input id="comment_image" type="file" wire:model="new_comments.{{ $post->id }}.image" class="d-none" accept="image/png, image/gif, image/jpeg">
</form>
@if ($new_comments[$post->id]['image'] ?? false)
   <div class="d-flex justify-content-center">
      <img src="{{ $new_comments[$post->id]['image']->temporaryUrl() }}" class="w-50">
   </div>
@endif

but when i try uploading an image in the post number 1 for example, it always gets added in the last item of the array and i don't know why!

enter image description here

in this case i uploaded the image in the first post but magically its in the last item of the array

(you can notice that the other model is working perfectly fine!)

Can anyone help me with this problem? Thank you.

0 Answers
Related