Route:
$router->post('/add_rewards', ['as' => 'add_reward', 'uses' => 'Controller@addReward', ]);
View to upload the Image:
1) add [ enctype="multipart/form-data” ] to the HTML form. example:
<form class="form-horizontal" action="{{route('add_reward')}}" method="post" enctype="multipart/form-data">
2) add the image file upload [ type=file ] example:
<div class="form-group"> <div class="col-xs-12"> <div class="form-material form-material-primary floating input-group"> <input class="form-control btn-file" type="file" id="reward-image" name="reward-image"> <span class="input-group-addon"><i class="fa fa-file-excel-o"></i></span> </div> </div> </div>
Controller:
// check if file was uploaded if ($request->hasFile('reward-image')) { // get the file object $file = $request->file('reward-image'); // set the upload path (starting form the public path) $rewardsUploadPath = '/uploads/rewards/images/'; // create a unique name for this file $fileName = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString()) . '-' . str_random(5) . '.' . $file->getClientOriginalExtension(); // move the uploaded file to its destination $file->move(public_path() . $rewardsUploadPath, $fileName); // save the file path and name $filePathAndName = $rewardsUploadPath . $fileName; }
Save the Image on the object:
$reward = new Reward(); $reward->image = $image; $reward->save();
View to display the image:
<td class="text-center"> <img src="{{$reward->image}}" width="40px;"/> </td>