为什么某些属性没有保存在我的数据库中?

时间:2018-01-18 02:19:32

标签: php mysql laravel-5

我是Laravel和MySql的新手,实在无法看到我的错误。

当我尝试创建另一个用户,然后我检查我的数据库时只保存了一些属性,但是" last_name"," user"," type&# 34;,"活跃"和"地址"没有得救。

另外两个用户在phpMyAdmin中注册,这就是他们完成的原因。

这是图像:

enter image description here

这是我桌子的迁移文件:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name', 100);
        $table->string('last_name', 100);
        $table->string('email');
        $table->string('user', 50);
        $table->string('password', 60);
        $table->enum('type', ['user', 'admin']);
        $table->boolean('active');
        $table->text('address');
        $table->rememberToken();
        $table->timestamps();
    });
}

这是我的"商店"我的UserController中的方法:

public function store(SaveUserRequest $request)
{
    $data = [
        'name'          => $request->get('name'),
        'last_name'     => $request->get('last_name'),
        'email'         => $request->get('email'),
        'user'          => $request->get('user'),
        'password'      => $request->get('password'),
        'type'          => $request->get('type'),
        'active'        => $request->has('active') ? 1 : 0,
        'address'       => $request->get('address')
    ];
    $user = User::create($data);
    $message = $user ? 'Usuario agregado correctamente!' : 'El usuario NO pudo agregarse!';

    return redirect()->route('admin.user.index')->with('message', $message);
}

这是我的申请文件:

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class SaveUserRequest extends Request
{
     /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name'      => 'required|max:100',
            'last_name' => 'required|max:100',
            'email'     => 'required|email|unique:users',
            'user'      => 'required|unique:users|min:4|max:20',
            'password'  => 'required|confirmed',
            'type'      => 'required|in:user,admin'
        ];
    }
}

这是我的create.blade.php:

@extends('admin.template')

@section('content')

<div class="container text-center">

    <div class="page-header">
        <h1>
            <i class="fa fa-user"></i> USUARIOS <small>[ Agregar usuario ]</small>
        </h1>
    </div>

    <div class="row">
        <div class="col-md-offset-3 col-md-6">

            <div class="page">

                @if (count($errors) > 0)
                    @include('admin.partials.errors')
                @endif

                {!! Form::open(['route'=>'admin.user.store']) !!}

                    <div class="form-group">
                        <label for="name">Nombre:</label>

                        {!!
                            Form::text(
                                'name',
                                null,
                                array(
                                    'class'=>'form-control',
                                    'placeholder' => 'Ingresa el nombre...',
                                    'autofocus' => 'autofocus',
                                    //'required' => 'required'
                                )
                            )
                        !!}
                    </div>

                    <div class="form-group">
                        <label for="last_name">Apellidos:</label>

                        {!!
                            Form::text(
                                'last_name',
                                null,
                                array(
                                    'class'=>'form-control',
                                    'placeholder' => 'Ingresa los apellidos...',
                                    //'required' => 'required'
                                )
                            )
                        !!}
                    </div>

                    <div class="form-group">
                        <label for="email">Correo:</label>

                        {!!
                            Form::text(
                                'email',
                                null,
                                array(
                                    'class'=>'form-control',
                                    'placeholder' => 'Ingresa el correo...',
                                    //'required' => 'required'
                                )
                            )
                        !!}
                    </div>

                    <div class="form-group">
                        <label for="user">Usuario:</label>

                        {!!
                            Form::text(
                                'user',
                                null,
                                array(
                                    'class'=>'form-control',
                                    'placeholder' => 'Ingresa el nombre de usuario...',
                                    //'required' => 'required'
                                )
                            )
                        !!}
                    </div>

                    <div class="form-group">
                        <label for="password">Password:</label>

                        {!!
                            Form::password(
                                'password',
                                array(
                                    'class'=>'form-control',
                                    //'required' => 'required'
                                )
                            )
                        !!}
                    </div>

                    <div class="form-group">
                        <label for="confirm_password">Confirmar Password:</label>

                        {!!
                            Form::password(
                                'password_confirmation',
                                array(
                                    'class'=>'form-control',
                                    //'required' => 'required'
                                )
                            )
                        !!}
                    </div>

                    <div class="form-group">
                        <label for="type">Tipo:</label>

                        {!! Form::radio('type', 'user', true) !!} User
                        {!! Form::radio('type', 'admin') !!} Admin
                    </div>

                    <div class="form-group">
                        <label for="address">Dirección:</label>

                        {!!
                            Form::textarea(
                                'address',
                                null,
                                array(
                                    'class'=>'form-control'
                                )
                            )
                        !!}
                    </div>

                    <div class="form-group">
                        <label for="active">Active:</label>

                        {!! Form::checkbox('active', null, true) !!}
                    </div>

                    <div class="form-group">
                        {!! Form::submit('Guardar', array('class'=>'btn btn-primary')) !!}
                        <a href="{{ route('admin.user.index') }}" class="btn btn-warning">Cancelar</a>
                    </div>

                {!! Form::close() !!}

            </div>

        </div>
    </div>

</div>
@stop

这是我的请求的var_dump的值:

array(8){[&#34; name&#34;] =&gt; string(4)&#34; Joel&#34; [&#34;姓氏&#34;] =&GT; string(8)&#34; Bautista&#34; [&#34;电子邮件&#34;] =&GT; string(17)&#34; joe23@hotmail.com" [&#34;用户&#34;] =&GT; string(5)&#34; joe23&#34; [&#34;密码&#34;] =&GT; string(6)&#34; 123456&#34; [&#34;类型&#34;] =&GT; string(5)&#34; admin&#34; [&#34;有源&#34;] =&GT; int(1)[&#34;地址&#34;] =&gt; string(15)&#34;无话可说。&#34; }

public function store(SaveUserRequest $request)
{
    $data = [
        'name'          => $request->get('name'),
        'last_name'     => $request->get('last_name'),
        'email'         => $request->get('email'),
        'user'          => $request->get('user'),
        'password'      => $request->get('password'),
        'type'          => $request->get('type'),
        'active'        => $request->has('active') ? 1 : 0,
        'address'       => $request->get('address')
    ];
    $user = User::create($data);

    var_dump($data);
    die();

    //$message = $user ? 'Usuario agregado correctamente!' : 'El usuario NO pudo agregarse!';
    //return redirect()->route('admin.user.index')->with('message', $message);
}

1 个答案:

答案 0 :(得分:2)

检查用户模型(User.php)的可填写属性。在使用create方法时,您需要在模型上指定可填充或保护属性。请检查并尝试在fillable属性中添加其他列,需要使用&#34; create&#34;方法。