That is code to store an image just make sure the path is correct.
Nov 11, 2019
7
Level 1
Store image
How do we store an image into the database? Because in the tutorial I follow, it only upload it
https://www.webslesson.info/2018/02/image-file-upload-in-laravel-with-validation.html
index.blade.php
<td>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>
<body>
<br />
@if (count($errors) > 0)
<div class="alert alert-danger">
Upload Validation Error<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@if ($message = Session::get('success'))
<div class="alert alert-success alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{{ $message }}</strong>
</div>
<img src="/images/{{ Session::get('path') }}" width="300" />
@endif
<form method="post" action="{{url('/masterStockist')}}" enctype="multipart/form-data">
{{ csrf_field() }}
<table class="table">
<p width="30"><input type="file" name="select_file" /></p>
<p width="30%" align="left"><input type="submit" name="upload" class="btn btn-primary" value="Upload"><span class="text-muted col-md-2">jpg, png, jpeg</span></p>
</table>
</form>
</body>
</html>
</td>
UploadPictureController.php
function upload(Request $request)
{
$this->validate($request, [
'select_file' => 'image|max:2048'
]);
$image = $request->file('select_file');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
return redirect()->back()->with('success', 'Image Uploaded Successfully')->with('path', $new_name);
}
web.php
Route::post('/masterStockist', 'UploadPictureController@upload');
Migration
$table->string('image')->nullable();
Please or to participate in this conversation.