anonymouse703's avatar

Livewire:How to store image url in database?

Hi guys I have to image to upload (profile and sketch), What is the best way to upload it in database?

So far, this is what I did but it's definitely missing some code.

public function store(){

        $action = '';

        $data = $this->validate([
            'gender' => 'required',
            '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_no1' => 'required | integer',
            'cell_no2' => 'required | integer',
            'employer' => 'required',
            'employer_address' => 'required',
            'employer_contact' => 'required'
        ]);

            $this->profile->storage('images','public');
            $this->sketch->storage('images','public');

        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
neilstee's avatar

@anonymouse703 I don't think saving the image itself on the database is a good idea.

Looks like you are on the right track, just save the file into a folder and save the filename into the DB so you can reference it later when needed.

neilstee's avatar

@anonymouse703 there are a ton of examples when you search it.

Basically what you just need is to get the path where you save your file, and also the filename, then save that to the database.

Read more about the link I have sent to you, you'll learn as you go along.

Snapey's avatar

no different to doing it in regular laravel, for which there are thousands of tutorials

chaudigv's avatar

Something like this?

$path =  $this->profile->storage('images','public');

$user->profile_pic = $path;
$user->Save();

Please or to participate in this conversation.