$filename = $this->product_image->getClientOriginalName();
$this->product_image->storeAs('product', $filename);
ProductImage::create([
'product_image' => $filename,
// ...
Jan 6, 2022
5
Level 5
Livewire file upload changes the file name in the database
I am using Livewire to handle my file uploads, but I have it set up to leave the file name as the original that the client will be using. However, when the file is saved into the Database it always adds the word product/ to the beginning of the name but the actual file that is loaded into the folder is just the file name so the image does not render on the frontend. Here is my code for storing the image:
protected $rules = [
'product_image'=> 'required | image',
'altTag' => 'required|max:255',
];
private function resetForm() {
$this->product_image = '';
$this->altTag = '';
}
public function store() {
$this->validate();
$filename = $this->product_image->getClientOriginalName();
ProductImage::create([
'product_image' => $this->product_image->storeAs('product', $filename),
'image_alt_tag' => $this->altTag,
'image_title' => Str::replace('.jpg', '', $filename),
'product_id' => $product_id,
]);
$this->resetForm();
}
So if I upload a file for example called test.jpg, in the Database the name becomes product/test.jpg but in the file directory, it stays as test.jpg. I tried to remove it with Str::replace but that did not work either.
Level 104
Please or to participate in this conversation.