iamlux20's avatar

File upload showing null

I've tried to follow what the documentation contains however I'm still getting null on every type of file I tried to upload.

My Livewire file

<form wire:submit.prevent="uploadData" enctype="multipart/form-data">
    <div class="modal-body">

        <div class="row justify-content-center">
            

            <!--begin::Form-->
            <div class="col-8">
                <div class="form-group">
                    <input type="file" wire:model="file" class="form-control">
                </div>
            </div>

            <!--end::Form-->
        </div>
    </div>

    <div class="modal-footer">
        <button type="button" class="btn btn-light" data-bs-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary">Upload</button>
    </div>
</form>

Livewire Controller

use App\Models\User;
use Livewire\Component;
use Google\Cloud\Firestore\FirestoreClient;
use Livewire\WithFileUploads;

class Uploader extends Component
{
    use WithFileUploads;

    
    public $file;

    // trimmed all other functions

    public function uploadData()
    {
        dd($this->file);
    }


0 likes
13 replies
Snapey's avatar

how does uploadModelStock() get called?

iamlux20's avatar

@Snapey forgot to rename that to generic upload(), it will get called once I've clicked submit

Snapey's avatar

check that you have livewire scripts loaded on this page

check that there is a network request as soon as you select a file

iamlux20's avatar

@Snapey checked livewireScripts() and it's loaded. I've also tried making a separate blade page for file upload but problem still remains with uploadData() seeing null on $this->file

MoamenEltouny's avatar

This code snippet working fine with me but i have a question is there another code lines below this snippet in your Uploader component ?

iamlux20's avatar

@Pharaonic it's actually irrelevant to show mount and hydrate since it only re-instantiates the FirestoreClient the project is in.

for render:

public function render()
{
	$items  = $this->items;
	return view('livewire.inventory.uploads', compact('items'));
}

public function uploadData()
{
		// attempt to open uploaded CSV file
				// problem is here, where it is throwing null data on the model $file

		// convert CSV line-per-line into sets of array
		while(($data = fgetcsv($fopen, 10000, ',')) !== FALSE)
		{
				$items[] = $data;
		}
		$this->items = $items;
}
MoamenEltouny's avatar

@iamlux20 so even if you use this snippet giving you null too?

public function updatedFile($value)
{
    dd($value);
}
iamlux20's avatar
iamlux20
OP
Best Answer
Level 2

Tagging this one as partly solved. For some reason, Livewire temporarily uploads the file (as POST request, seen in my debugbar) into storage/app/livewire-tmp. I then use $file->getPath().'/'.$file->getFilename() so I can use fopen(). I am now able to access the file contents

1 like

Please or to participate in this conversation.