获取除laravel错误中的图像以外的所有数据

时间:2018-08-05 04:54:26

标签: laravel laravel-5

      <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>

强文本

当我dd($ request-> all());在控制器的存储功能中,我得到了所有其他值,但没有图像。。当我dd()时,它甚至不显示image = null。 array:8 [“ _token” =>“ C27oLkk8wDTcBnZDA38nPCte4SY18HqXqxcFDcrI”“ name” => null“ dateofbirth” => null“ gender” =>“ Male”“地址” => null“ email” => null“ phone” => null “ course” => null]

我正在使用ajax请求,尽管我输入了图像,但显示该图像是必需的。我在这里做错什么了?

       $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,
]);

1 个答案:

答案 0 :(得分:0)

您可以尝试

$validator = Validator::make($request, [
    'name' => 'required|min:2',
    'dateofbirth' => 'required|date',
    'gender' => 'required',
    'address' => 'required',
    'email' => 'required|email',
    'phone' => 'required|numeric',
    'course' => 'required',

    'image' => 'required|file|mimetypes:image/jpeg,image/png',
]);


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

//store image 
$path = $request->image->store('images'); 
//it will store your images at `storage/app/public/images` folder with dynamically generated image name, be sure create images folder there


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

从命令行执行此命令

php artisan storage:link

它将创建storage/app/publicpublic/storage文件夹的符号链接。

现在您可以在这样的视图中使用图像

{{asset('storage/images/test.jpeg')}}

如果您必须在控制器中读取该文件,则可以像这样使用它

Storage::disk('public')->get('images/test.jpeg');

始终将文件存储在存储文件夹中。所有公共文件都应存储在storage/app/public

有关详细信息,请点击此处https://laravel.com/docs/5.6/filesystem#introduction