I need help in saving uploaded file name in database table using laravel 5.1.
My Controller code for saving Image details
public function store(Request $request)
{
if($request->hasFile('img_filename'))
{
$destinationPath="offerimages";
$file = $request->file('img_filename');
$filename=$file->getClientOriginalName();
$request->file('img_filename')->move($destinationPath,$filename);
}
$input=$request->all();
Offer_image::create($input);
return redirect('offerimage');
}
My view code for accepting image
{!! Form::open(array('route'=>'offerimage.store','role'=>'form','files'=>true)) !!}
<div class="box-body">
<div class="form-group">
{!! Form::label('img_name','Name') !!}
{!! Form::text('img_name', $value = null, $attributes = array('class'=>'form-control','id'=>'img_name','required')) !!}
</div>
<div class="form-group">
{!! Form::label('img_description','Description') !!}
{!! Form::textarea('img_description', $value = null, $attributes = array('class'=>'form-control','id'=>'img_description','required')) !!}
</div>
<div class="form-group">
{!! Form::label('img_filename','Upload Image') !!}
{!! Form::file('img_filename') !!}
</div>
{!! Form::hidden('status',$value='active') !!}
</div><!-- /.box-body -->
<div class="box-footer">
{!! Form::submit('Submit',$attributes=array('class'=>'btn btn-primary')) !!}
</div>
{!! Form::close() !!}
This controller code to store image working properly, but where i am trying to save image file name to table , this code is storing filepath to database table.
As I am using direct create() method to store the request object in table, I don't know how do I store file name instead of path.
Check this Image for table data
The Problem is that your Request Data hasn't changed while you uploaded the picture. So img_filename
still contains tmpdata.
You can try this:
$input = $request->all();
$input['img_filename'] = $filename;
Code that works for me :
$updir = 'images/';
$img_name = 'image.jpeg';
Request::file('img_filename')->move($updir, $img_name);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With