得到错误消息一般错误:1364字段“性别”没有默认值

时间:2019-07-01 18:56:56

标签: laravel laravel-5.8

我正在尝试创建用户个人资料注册。但是,我遇到了错误,但是看不到我在做什么错。

  

一般错误:1364字段“性别”没有默认值

迁移

public function up()
{
    Schema::create('profiles', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedBigInteger('user_id')->index();
        $table->string('gender');
        $table->string('initials');
        $table->string('first_name');
        $table->string('last_name');
        $table->string('profession');
        $table->string('specialism');
        $table->string('profession_titulature');
        $table->string('company_name');
        $table->string('country');
        $table->string('city');
        $table->string('postal_code');
        $table->string('street');
        $table->string('house_number');
        $table->string('house_number_addition');
        $table->string('phone_number');
        $table->string('mobile_number');
        $table->longText('description');
        $table->string('agb_code');
        $table->string('big_code');
        $table->boolean('accepted')->default(1);
        $table->boolean('visible')->default(1);
        $table->timestamps();

        // This is foreignkey at the database level
        $table->foreign('user_id')->references('id')->on('users');
    });
}

请求

public function rules()
{
    return [
        'gender' => 'required',
        'initials' => '',
        'first_name' => '',
        'last_name' => '',
        'profession' => '',
        'specialism' => '',
        'profession_titulature' => '',
        'company_name' => '',
        'country' => '',
        'city' => '',
        'postal_code' => '',
        'street' => '',
        'house_number' => '',
        'house_number_addition' => '',
        'phone_number' => '',
        'mobile_number' => '',
        'agb_code' => '',
        'big_code' => '',
        'description' => '',
    ];
}

表格

<form action="{{ route('profile.store') }}" method="post">
    @csrf
    <div class="form-row">
        <div class="form-group col-md-6">
            <label for="selectGender">Gender</label>
            <select name="gender" class="form-control" id="selectGender">
                <option value="" disabled>Select gender</option>
                @foreach($profile->activeOptions() as $activeOptionKey => $activeOptionValue)
                    <option value="{{$activeOptionKey}}" {{ $profile->active == $activeOptionValue ? 'selected' : '' }}>
                        {{ $activeOptionValue }}\
                    </option>
                @endforeach
            </select>
            @if($errors->has('gender'))
                <span class="invalid-feedback" style="display: block;" role="alert">
                        <strong>{{ $errors->first('gender') }}</strong>
                    </span>
            @endif
        </div>
        <div class="form-group col-md-6">
            <label for="inputInitials">Initials</label>
            <input name="initials" type="text" class="form-control" id="inputInitials"
                   placeholder="Initials" value="{{old('initials') ?? ''}}">
            @if($errors->has('initials'))
                <span class="invalid-feedback" style="display: block;" role="alert">
                        <strong>{{ $errors->first('initials') }}</strong>
                    </span>
            @endif
        </div>
        <!-- I have not put the whole form here... -->
        <button type="submit" class="btn btn-primary">Opslaan</button>
    </div>
</form>

模型

class Profile extends Model
{
    protected $guarded = [];
}

控制器

public function create()
{
    $profile = new Profile();
    return view('pages.profile.create', compact('profile'));
}


public function store(StoreProfileRequest $request)
{
    $validated = $request->validated();
    Profile::create(['user_id' => Auth::id()], $validated);

    return redirect('/');
}

1 个答案:

答案 0 :(得分:0)

我认为问题在这里

 Profile::create(['user_id' => Auth::id()], $validated);

雄辩的 create 方法需要一个assoc数组,您在这里放置了两个数组:)

但是要创建具有大规模归因的模型,您需要在模型上指定$fillable属性。

请检查有关此文件的文档: https://laravel.com/docs/5.8/eloquent#mass-assignment