Reputation: 135
I am stuck with image uploading in Laravel 5.6. text input is fine but image not uploading.
For testing route and input field, I put a demo echo for retrieve text column data and it show perfectly. But the image is not uploading...
But I used same code in my previous project to image uploading and that was good..this time is not working.
controller-
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator;
class UserController extends Controller
{
public function insertChildren(Request $request){
echo $request->input('firstName');
//this echo doing well
if ($request->hasFile('image')) {
$imageExtention=$request->file('image')->getClientOriginalExtension();
$randomString=substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 10)), 0, 10);
$modifiedImageName=$randomString.'.'.$imageExtention;
$productImage->move(public_path('img'),$modifiedImageName);
}
}
Form
<form role="form" action="{{url('/children/add')}}" method="post">
@csrf
<div class="form-group ml-4 mr-4">
<div class="row">
<div class="col-6">
<label for="name">First Name</label>
<input value="{{ old('firstName') }}" name="firstName" type="text" class="form-control" id="first" placeholder="First Name">
</div>
<div class="col-6">
<label for="name">Last Name</label>
<input value="{{ old('lastName') }}" name="lastName" type="text" class="form-control" id="last" placeholder="Last Name">
</div>
</div>
</div>
<div class="form-group ml-4 mr-4">
<label for="gender">gender</label>
<br>
<input value="{{ old('gender') }}" type="text" name="gender" class="form-control" placeholder="Boy or Girl or Other">
</div>
<div class="form-group ml-4 mr-4">
<label for="Birthday">Birthday</label>
<input name="birthday" type="date" class="form-control" id="birthday">
</div>
<div class="form-group ml-4 mr-4">
<label for="image">Profile Image</label>
<input type="file" name="image" class="form-control" id="image">
</div>
<button type="submit" style="background-color:#2DAE60;" class="btn">
<i class="fa fa-plus"></i> ADD </button>
</form>
Upvotes: 1
Views: 297
Reputation: 3467
You're missing enctype
attribute on your form:
<form role="form" action="{{url('/children/add')}}" method="post" enctype="multipart/form-data">
multipart/form-data
allows entire files to be included in the request data. For writing forms, that's all you need to know really.
Upvotes: 4