anonymouse703's avatar

Livewire: How to get the image name?

Hello guys, I upload a record with image but the filename of the image is something like this C:\Windows\Temp\php5C1D.tmp and the the file name.

this is my store function

public function store(){


        $action = '';

        $data = $this->validate([
            'profile' => 'image|max:1024',
            'last_name' => 'required',
            'first_name' => 'required',
            'middle_name' => 'required',
            'street' => 'required',
            'barangay' => 'required',
            'city' => 'required',
            'province' => 'required',
            'sketch' => 'image|max:1024',
            'birth_date' => 'required | date',
            'gender_id' => 'required',
            'civil_status_id' => 'required',
            'spouse' => 'required',
            'tel_no' => 'required',
            'cell_no_1' => 'required',
            'cell_no_2' => 'required',
            'employer' => 'required',
            'employer_address' => 'required',
            'employer_contact_no' => 'required'
        ]);
    
        if(!empty($this->profile)){
            $this->profile->store('public/images');
        }

        if(!empty($this->sketch)){
            $this->sketch->store('public/images');
        }


        if($this->customerId){
            Customer::find($this->customerId)->update($data);
            $action = 'edit';
        }else{
            Customer::create($data);
            $action = 'store';
        }
        $this->emit('showEmitedFlashMessage', $action);
        $this->resetInputFields();
        $this->emit('refreshParent');
        $this->emit('closeCustomerModal');

    }
0 likes
6 replies
rovshena's avatar
rovshena
Best Answer
Level 2

public function store(){

	$action = '';

	$data = $this->validate([
		'profile' => 'image|max:1024',
		'last_name' => 'required',
		'first_name' => 'required',
		'middle_name' => 'required',
		'street' => 'required',
		'barangay' => 'required',
		'city' => 'required',
		'province' => 'required',
		'sketch' => 'image|max:1024',
		'birth_date' => 'required | date',
		'gender_id' => 'required',
		'civil_status_id' => 'required',
		'spouse' => 'required',
		'tel_no' => 'required',
		'cell_no_1' => 'required',
		'cell_no_2' => 'required',
		'employer' => 'required',
		'employer_address' => 'required',
		'employer_contact_no' => 'required'
	]);

    if(!empty($this->profile)){
		$filename = 'profile.' . time() . $this->profile->getClientOriginalExtension();

		$this->profile->storeAs('public/images', $filename);

		$data['profile'] = $filename;
	}

	if(!empty($this->sketch)){
		$filename = 'sketch.' . time() . $this->sketch->getClientOriginalExtension();

		$this->sketch->storeAs('public/images', $filename);

		$data['sketch'] = $filename;
	}

	if($this->customerId){
		Customer::find($this->customerId)->update($data);
		$action = 'edit';
	}else{
		Customer::create($data);
		$action = 'store';
	}

	$this->emit('showEmitedFlashMessage', $action);
	$this->resetInputFields();
	$this->emit('refreshParent');
	$this->emit('closeCustomerModal');

}
anonymouse703's avatar

Hello sir, But this is livewire and request is not recognize and it will throw Undefined variable: request

anonymouse703's avatar

still got this Undefined property: Livewire\TemporaryUploadedFile::$getClientOriginalExtension

rovshena's avatar

I have forgotten to put brackets after getClientOriginalExtension method.

Snapey's avatar

You need to get the filename that was created by the store method

        if(!empty($this->profile)){
            $data['profile'] = $this->profile->store('public/images');
        }

        if(!empty($this->sketch)){
            $data['sketch'] = $this->sketch->store('public/images');
        }

Please or to participate in this conversation.