Reputation: 922
Hi I am New to Laravel I have done Upload a Image to path img/banner/{{image upload here}}
My doubt is here is to save the image path in database with which
user uploaded
And while retrieving data how to give my URL path here.
I have created a database name uploads
id user_id group_id filename extension filesize location created_at updated_at
Blade File
<div class="panel panel-primary">
<div class="panel-heading"><h2>Slide Image Upload</h2></div>
<div class="panel-body">
@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="/img/banner/{{ Session::get('image') }}">
@endif
@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form action="{{ route('image.upload.post') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="row">
<div class="col-md-6">
<input type="file" name="image" class="form-control">
</div>
<div class="col-md-6">
<button type="submit" class="btn btn-success">Upload</button>
</div>
</div>
</form>
</div>
</div>
Controller:
public function imageUploadPost()
{
request()->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$imageName = time().'.'.request()->image->getClientOriginalExtension();
request()->image->move(public_path('img/banner'), $imageName);
return back()
->with('success','You have successfully upload image.')
->with('image',$imageName);
}
Routes:
Route::post('/imageupload', 'Admin\ImageController@imageUploadPost')->name('image.upload.post');
Any Help will be appreciated.
Upvotes: 0
Views: 1320
Reputation: 1661
I am expecting that you have included namespace, if not
use DB;
$full_path = public_path('img/banner');
$ext = request()->image->getClientOriginalExtension();
$size = request()->image->getSize();
DB::table('uploads')->insert(
['group_id' => '1', user_id' => '1', 'filename' => $imageName,'filesize'=>$size ,'extension'=> $ext,'location' => $full_path,'created_at'=> date('Y-m-d H:m:s')]
);
You can get by this your database values, $imagename is the name you used to upload the image, not sure about your group_id so set to 1.
latest Edit
this is how you can get imageName:
$imageName = time().'.'.request()->image->getClientOriginalExtension();
froom your code only
Upvotes: 1