@shyann
In Laravel, you can easily upload an image into the database by following these steps:
Here is an example code snippet to illustrate the steps described above:
View file (create.blade.php):
1 2 3 4 5 |
<form method="POST" action="{{ route('image.store') }}" enctype="multipart/form-data"> @csrf <input type="file" name="image"> <button type="submit">Upload Image</button> </form> |
Controller file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
use AppModelsImage; public function store(Request $request) { $imageName = time().'.'.$request->image->getClientOriginalExtension(); $request->image->storeAs('images', $imageName); $imageData = file_get_contents($request->image); Image::create([ 'image' => $imageData, // other columns ]); return redirect()->route('images.index'); } |
Please note that storing images directly in the database can lead to performance issues and is not recommended for large files. It is usually more efficient to store the image files on disk and store the file path in the database instead.