Error with default values

1

You see, I have the following table User:

Schema::create('users', function(Blueprint $table){
        $table->increments('id');
        $table->string('name');
        $table->boolean('sexo');
        $table->string('email')->unique();
        $table->integer('saldo')->default(0);
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });

Notice the balance variable, which has a default value.

Here are the variables that will be introduced in the form:

protected $fillable=[
    'name','email','password','sexo'
];

This is the code of the form that the user will create:

@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">Registrarse</div>

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

                        <div class="form-group row">
                            <label for="name" class="col-md-4 col-form-label text-md-right">Nombre</label>

                            <div class="col-md-6">
                                <input id="name" type="text" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" name="name" value="{{ old('name') }}" required autofocus>

                                @if ($errors->has('name'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('name') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="sexo" class="col-md-4 col-form-label text-md-right">Sexo</label>

                            <div class="col-md-6">
                                Hombre <input type="radio" name="sexo" value=1 checked><br>
                                Mujer <input type="radio" name="sexo" value=0>

                                @if ($errors->has('sexo'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('sexo') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="email" class="col-md-4 col-form-label text-md-right">Correo electrónico</label>

                            <div class="col-md-6">
                                <input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" required>

                                @if ($errors->has('email'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('email') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="password" class="col-md-4 col-form-label text-md-right">Contraseña</label>

                            <div class="col-md-6">
                                <input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" required>

                                @if ($errors->has('password'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('password') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="password-confirm" class="col-md-4 col-form-label text-md-right">Confirmar contraseña</label>

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

                        <div class="form-group row mb-0">
                            <div class="col-md-6 offset-md-4">
                                <button type="submit" class="btn btn-primary">
                                    Crear cuenta de usuario
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

And the creation of the user is handled by this code:

protected function create(array $data){
    return User::create([
        'name'=>$data['name'],
        'email'=>$data['email'],
        'sexo'=>$data['sexo'],
        'password'=>Hash::make($data['password']),
    ]);
}

A user should be created which will have a balance of 0, but this error message appears:

How do I solve this so that they allow me to create my user?

    
asked by Miguel Alparez 17.06.2018 в 12:30
source

1 answer

1

I comment you should leave it as follows:

$table->integer('saldo')->default('0');

In addition to mentioning it in your property fillable

protected $fillable=[
    'name','email','password','sexo','saldo'
];

Finally, do the migration so that the changes take effect

php artisan migrate:fresh
    
answered by 17.06.2018 / 14:39
source