samedi 4 août 2018

getting all the data except image in laravel error

      <form id="add-record" enctype="multipart/form-data">
        @csrf
        <div class="success">

        </div>
        <div class="form-group row">
            <label for="name" class="col-sm-3 label-name col-form-label">Full                   Name</label>
        <div class="col-sm-9 input-group">
            <div class="input-group-prepend">
                <span class="input-group-text"><i class="fas fa-user"></i></span>
            </div>
            <input type="text" class="form-control input" id="name" name="name" placeholder="Full Name">
        </div>
    </div>
    <div class="row error">
        <div class="col-sm-9 offset-sm-3">
            <span class="text-danger" id="name-error"></span>
        </div>
    </div>
    <div class="form-group row">
        <label for="datepicker" class="col-sm-3 col-form-label">Date of birth</label>
        <div class="col-sm-9 input-group">
            <div class="input-group-prepend">
                <span class="input-group-text"><i class="fas fa-calendar"></i></span>
            </div>
            <input type="text" class="form-control input" id="datepicker" name="dateofbirth" placeholder="">
        </div>
    </div>
    <div class="row error">
        <div class="col-sm-9 offset-sm-3">
            <span class="text-danger" id="dateofbirth-error"></span>
        </div>
    </div>
    <div class="form-group row">
        <label for="course" class="col-sm-3 col-form-label">Gender</label>
        <div class="col-sm-9 input-group">
            <div class="form-check form-check-inline">
                <input class="form-check-input" type="radio" name="gender" id="male" value="Male" checked>
                <label class="form-check-label" for="male">
                    Male
                </label>
            </div>
            <div class="form-check form-check-inline">
                <input class="form-check-input" type="radio" name="gender" id="female" value="Female">
                <label class="form-check-label" for="female">
                    Female
                </label>
            </div>
        </div>
    </div>
    <div class="form-group row">
        <label for="address" class="col-sm-3 col-form-label">Address</label>
        <div class="col-sm-9 input-group">
            <div class="input-group-prepend">
                <span class="input-group-text"><i class="fas fa-home"></i></span>
            </div>
            <input type="text" class="form-control input" id="address" name="address" placeholder="Address">
        </div>
    </div>
    <div class="row error">
        <div class="col-sm-9 offset-sm-3">
            <span class="text-danger" id="address-error"></span>
        </div>
    </div>
    <div class="form-group row">
        <label for="email" class="col-sm-3 col-form-label">Email</label>
        <div class="col-sm-9 input-group">
            <div class="input-group-prepend">
                <span class="input-group-text"><i class="fas fa-envelope"></i></span>
            </div>
            <input type="email" class="form-control input" id="email" name="email" placeholder="yourmail@email.com">
        </div>
    </div>
    <div class="row error">
        <div class="col-sm-9 offset-sm-3">
            <span class="text-danger" id="email-error"></span>
        </div>
    </div>
    <div class="form-group row">
        <label for="phone" class="col-sm-3 col-form-label">Phone Number</label>
        <div class="col-sm-9 input-group">
            <div class="input-group-prepend">
                <span class="input-group-text"><i class="fas fa-phone"></i></span>
            </div>
            <input type="text" class="form-control input" id="phone" name="phone" placeholder="+3784773847">
        </div>
    </div>
    <div class="row error">
        <div class="col-sm-9 offset-sm-3">
            <span class="text-danger" id="phone-error"></span>
        </div>
    </div>
    <div class="form-group row">
        <label for="course" class="col-sm-3 col-form-label">Course Name</label>
        <div class="col-sm-9 input-group">
            <div class="input-group-prepend">
                <span class="input-group-text"><i class="fas fa-book-open"></i></span>
            </div>
            <input type="text" class="form-control input" id="course" name="course" placeholder="Course name">
        </div>
    </div>
    <div class="row error">
        <div class="col-sm-9 offset-sm-3">
            <span class="text-danger" id="course-error"></span>
        </div>
    </div>
    <div class="form-group row">
        <label for="image-file" class="col-sm-3 col-form-label">Your Image</label>
        <div class="col-sm-9 input-group">
            <input type="file" class="form-control-file input-image" name="image" id="image-file">
        </div>
    </div>
    <div class="row error">
        <div class="col-sm-9 offset-sm-3">
            <span class="text-danger" id="image-error"></span>
        </div>
    </div>
    <div class="form-group row">
        <div class="col-sm-9 offset-sm-3 input-group">
            <input type="submit" class="btn btn-primary">
        </div>
    </div>
</form>

strong text

When i dd($request->all()); in store function in controller, i get all other values but not image.. It even doesn't shows image=null when i dd(); array:8 [ "_token" => "C27oLkk8wDTcBnZDA38nPCte4SY18HqXqxcFDcrI" "name" => null "dateofbirth" => null "gender" => "Male" "address" => null "email" => null "phone" => null "course" => null ]

Iam using ajax request Although i input the image it shows the image is required.. What am i doing wrong here??

       $validator = Validator::make($request->all(), [
    'name' => 'required|min:2',
    'dateofbirth' => 'required|date',
    'gender' => 'required',
    'address' => 'required',
    'email' => 'required|email',
    'phone' => 'required|numeric',
    'course' => 'required',
    'image' => 'required|image|mimes:jpeg,png,jpg',
]);

   if($validator->fails()) {
    return response()->json([
        'errors' => $validator->errors(),
    ]);
  }

 $name = $request['name'];
$dateofbirth = $request['dateofbirth'];
$gender = $request['gender'];
$address = $request['address'];
$email = $request['email'];
$phone = $request['phone'];
$course = $request['course'];

if($request->hasFile('image')) {
    $img_name = time() . '.' .$request->file('image')->getClientOriginalExtension();
    $destinationPath = public_path('/assets/images/');
    $request->file('image')->move($destinationPath, $img_name);
    $image = $img_name;
}

Student::create([
    'name' => $name,
    'dateofbirth' => $dateofbirth,
    'gender' => $gender,
    'address' => $address,
    'email' => $email,
    'phone' => $phone,
    'course' => $course,
    'image' => $image,
]);



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire