没有默认值的laravel字段 [英] laravel field with no default value

查看:66
本文介绍了没有默认值的laravel字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个小问题,它是image字段没有编译器所说的默认值,但实际上我为其分配了一个值,我知道当该字段应该有一个值时会弹出此错误,但是程序员没有给它赋值,我知道所有这些,但是我已经给我的字段赋了值,所以我在这里错过了

i have a small problem here and it is that the image field doesn't have a default value as the complier said but actually i assigned a value for it i know that this error pops up when the field should have a value but the programmer didn't assign a value to it i know all of this but i already assigned a value to my field so what i am missing here

控制器

protected function validator(array $data)

    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
            'image' => ['required' , 'image' , 'mimes:jpeg,png,jpg', 'max:2048'],
        ]);
    }
 protected function create(array $data)
    {

        $imageName = time().'.'.request()->image->getClientOriginalExtension();
        request()->image->move(public_path('images'), $imageName);

        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'image' => $imageName,
            'password' => Hash::make($data['password']),
        ]);
    }

查看

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">{{ __('Register') }}</div>

                <div class="card-body">
                    <form method="POST" action="{{ route('register') }}" enctype="multipart/form-data">
                        @csrf

                        <div class="form-group row">
                            <label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>

                            <div class="col-md-6">
                                <input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>

                                @error('name')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

                            <div class="col-md-6">
                                <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email">

                                @error('email')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>

                            <div class="col-md-6">
                                <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="new-password">

                                @error('password')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>

                            <div class="col-md-6">
                                <input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
                            </div>
                        </div>

                            <label  class="col-md-4 col-form-label text-md-right" >Upload a picture</label>
                            <div class="custom-file mb-3 col-sm-6">
                            <input type="file" class="custom-file-input center" id="image" name="image">
                            <label class="custom-file-label" for="customFile"></label>
                            </div>

                        <div class="form-group row mb-0">
                            <div class="col-md-6 offset-md-4">
                                <button type="submit" class="btn btn-primary">
                                    {{ __('Register') }}
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

问题主要出在控制器上,但我说过,我放置视图是为了以防万一您想看到它

the problem is mainly from the controller but i said that i put the view just in case you want to see it

小注:图像正在上传,并且像代码中所说的那样存储在公用文件夹中,但是它的名称没有保存在数据库中,而且字段图像在我的数据库中是字符串,因为我只想要它的名称,这样我就可以查看

small note : the image is uploading and it is stored at public folder just like the code said but its name doesn't save at the database also the field image is string at my database because i just want its name so i could view it

快照

推荐答案

为您的图片字段设置默认图片.如果没有在模型User.php

Set a default image for your image field. Also make sure you have made it fillable, if not set it in protected $fillable in your model User.php

当用户不上传图片时,您会收到此错误.

You will get this error when a user doesn't upload an image.

$table->string('image')->default('default.jpg');//You can also add .png

现在您的存储中有一个默认图像,名称为 default.jpg
这样,如果用户不上传图片,该图片将具有默认值.

Now have a default image in your storage with name default.jpg
This way the image will have a default value if user doesn't' upload it.

这篇关于没有默认值的laravel字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆