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.