欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

How to save uploaded file name in table using Laravel 5.1 自样保存上传的文件名

shiping1 的头像

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;

来自  http://stackoverflow.com/questions/33150998/how-to-save-uploaded-file-name-in-table-using-laravel-5-1
普通分类: