Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save uploaded file name in table using Laravel 5.1

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

like image 504
dollar Avatar asked Oct 15 '15 14:10

dollar


2 Answers

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;
like image 83
darthsoup Avatar answered Sep 28 '22 15:09

darthsoup


Code that works for me :

$updir = 'images/';
$img_name = 'image.jpeg';
Request::file('img_filename')->move($updir, $img_name);
like image 33
fico7489 Avatar answered Sep 28 '22 15:09

fico7489