Laravel 5 File Upload With data

Laravel 5 is easy to use file upload is very easy to learn here is the code comment to more improvement.

In Your Views


{!! Form::open(
array(
'route' => 'your path',
'class' => 'form',
'novalidate' => 'novalidate',
'files' => true)) !!}

{!! Form::text(‘name’, null, array(‘placeholder’=>’ Title’, ‘class’=>’form-control’)) !!}
{!! Form::text(‘price’, null, array(‘placeholder’=>’Price’, ‘class’=>’form-control’)) !!}
{!! Form::file(‘img_link’, null) !!}
{!! Form::submit(‘Create Product!’, array(‘class’ => ‘btn btn-default’)) !!}

{!! Form::close() !!}

In Your Controller


public function store(Request $request)
{
if ($request->hasFile('img_link')) {
$file = $request->file('img_link');
$extension = $file->getClientOriginalExtension();
$desFolder = "uploads";
$fileName = rand(11111, 99999) . '.' . $extension;
$upload_sec = $file->move($desFolder,$fileName);
if($upload_sec){
$Product = new Product;
$Product->name = $request->name;
$Product->price = $request->price;
$Product->img_link = url('uploads').'/'.$fileName; // you can save it in you database with full path
// $destinationPath = '/public/images/';
$Product->save();
//return 'Image Uploaded';
Session::flash('update', 'Inserted');
return Redirect::to('products');
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s