How to save the name of uploaded image in databse

how to save the name of the uploaded file in database?

    use WithFileUploads;

    public $image;
    public $name;

    public function save()
    {
        $this->validate([
            'name' => 'required',
            'image' => 'required',
        ]);


        Coach::create([
            'name' => $this->name,
            'image' => $this->image, =>  /* how to save in db */
        ]);

        $this->image->store('coaches');

    }

When you save the image using the $this->save() directive the file name is returned. So you could put that before your database call, or you might even be able to inline it.

$filename = $this->image->store('coaches');

Coach::create([
    'name' => $this->name,
    'image' => $filename
]);
1 Like