-2

I am stuck trying to store an image of a product in the database. My database is currently just storing the name of the image, like chicken.jpeg. Not really a chicken by the way hahaha. So the current output is the filename of the image is stored in the database. However the desired result should be the whole image is stored in the database, that way after the image is uploaded through the form, there would be no need for the user to keep the image on their computer anymore. I have some code below if you feel interested to help me.

This is my ProductController@store:

public function store(Request $request)
{
    $data = request()->validate([
        'productName'            =>  'required',
        'productImage'              =>  'required',
        'productLink'      =>  'required',
        'productPrice'      =>  'required',
        'productDescription'      =>  'required',
    ]);
    $product = new Product([
        'productName'            =>    $request->get('productName'),
        'productImage'              =>    $request->get('productImage'),
        'productLink'      =>    $request->get('productLink'),
        'productPrice'      =>  $request->get('productPrice'),
        'productDescription'      =>  $request->get('productDescription'),
    ]);
    $product->save();   
    return redirect('/pr');
}

This is my form:

<form action="/storeProduct" method="post">
@csrf
  <label for="productName">Product Name:</label><br>
  <input type="text" id="productName" name="productName" autocomplete="off" value="{{ 
old('productName') }}"><br>
  @error('productName') <p style="color: red">{{ $message }}</p> @enderror
  <label for="productImage">Product Image:</label><br>
  <input type="file" id="productImage" name="productImage" autocomplete="off" value="{{ 
old('productImage') }}"><br>
  @error('productImage') <p style="color: red">{{ $message }}</p> @enderror
  <label for="productLink">Product Link:</label><br>
  <input type="text" id="productLink" name="productLink" autocomplete="off" value="{{ 
old('productLink') }}"><br>
 @error('productLink') <p style="color: red">{{ $message }}</p> @enderror
  <label for="productPrice">Product Price:</label><br>
  <input type="decimal" id="productPrice" name="productPrice" autocomplete="off" value="{{ 
old('productPrice') }}"><br>
  @error('productPrice') <p style="color: red">{{ $message }}</p> @enderror
  <label for="productDescription">Product Description:</label><br>
  <input type="text" id="productDescription" name="productDescription" autocomplete="off" value=" 
   {{ old('productDescription') }}"><br>
  @error('productDescription') <p style="color: red">{{ $message }}</p> @enderror
  <input type="submit" value="Submit">
</form>

Thanks for taking the time to read my question and attempting to help me. I have attached my GitHub repository below, if you are interested in reading through my code.

https://github.com/xiaoheixi/blog

  • You can check here [Upload Image](https://stackoverflow.com/questions/42755302/laravel-5-4-upload-image) – vinod Sep 16 '20 at 13:19
  • And also add `enctype="multipart/form-data"` in form tag. – vinod Sep 16 '20 at 13:21
  • It is often advised not to store (image) files in a database, but to store them as files on the server. You can then refer to their location in the database. There are several reasons for this: 1. Files are easier to access if they are files. 2. less database overhead. 3. Database doesn't get too big, easier on the backups. 4. I'm sure that one can think of more reasons. Sometimes there are probably also good reasons to store images in a database. It's up to you to make the right choice. – KIKO Software Sep 16 '20 at 13:26

1 Answers1

0

For uploading image through form you need to set enctype in your form first and also instead of uploading an image into database you should upload it to your public image folder it will be far better and store only name of image into your database

changes in form:

<form enctype='multipart/form-data' action="/storeProduct" method="post">
@csrf
  <label for="productName">Product Name:</label><br>
  <input type="text" id="productName" name="productName" autocomplete="off" value="{{ 
old('productName') }}"><br>
  @error('productName') <p style="color: red">{{ $message }}</p> @enderror
  <label for="productImage">Product Image:</label><br>
  <input type="file" id="productImage" name="productImage" autocomplete="off" value="{{ 
old('productImage') }}"><br>
  @error('productImage') <p style="color: red">{{ $message }}</p> @enderror
  <label for="productLink">Product Link:</label><br>
  <input type="text" id="productLink" name="productLink" autocomplete="off" value="{{ 
old('productLink') }}"><br>
 @error('productLink') <p style="color: red">{{ $message }}</p> @enderror
  <label for="productPrice">Product Price:</label><br>
  <input type="decimal" id="productPrice" name="productPrice" autocomplete="off" value="{{ 
old('productPrice') }}"><br>
  @error('productPrice') <p style="color: red">{{ $message }}</p> @enderror
  <label for="productDescription">Product Description:</label><br>
  <input type="text" id="productDescription" name="productDescription" autocomplete="off" value=" 
   {{ old('productDescription') }}"><br>
  @error('productDescription') <p style="color: red">{{ $message }}</p> @enderror
  <input type="submit" value="Submit">
</form>

and for uploading an image you can use

if ($request->hasFile('productImage')) {
    $image = $request->file('productImage');
    $name = time().'.'.$image->getClientOriginalExtension();
    $destinationPath = public_path('/images');
    $image->move($destinationPath, $name);
}

for file upload reference read this answer Laravel Image upload

Bhavin
  • 577
  • 6
  • 11