如何为App \ User添加列并将数据存储到laravel 5.1中的users表中?

时间:2015-12-18 06:22:22

标签: laravel authentication

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password', 60);
        $table->integer('mobile');
        $table->integer('status')->default(0);
        $table->integer('team_id');
        $table->string('created_by');
        $table->string('updated_by');
        $table->rememberToken();
        $table->timestamps();
    });
}

上面的方法创建了几个额外列的用户表。然后我在我的用户模型中添加了可填写内容:

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

我的create.blade.php视图是:

@extends('master')

@section('title','Creating.. User')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Register</div>
                <div class="panel-body">
                    @if (count($errors) > 0)
                        <div class="alert alert-danger">
                            <strong>Whoops!</strong> There were some problems with your input.<br><br>
                            <ul>
                                @foreach ($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                    @endif

                    <form class="form-horizontal" role="form" method="POST" action="{{ url('/auth/register') }}">
                        {!! csrf_field() !!}

                        <div class="form-group">
                            <label class="col-md-4 control-label">Name</label>
                            <div class="col-md-6">
                                <input type="text" class="form-control" name="name" value="{{ old('name') }}">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-md-4 control-label">E-Mail Address</label>
                            <div class="col-md-6">
                                <input type="email" class="form-control" name="email" value="{{ old('email') }}">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-md-4 control-label">Password</label>
                            <div class="col-md-6">
                                <input type="password" class="form-control" name="password">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-md-4 control-label">Confirm Password</label>
                            <div class="col-md-6">
                                <input type="password" class="form-control" name="password_confirmation">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-md-4 control-label">Mobile</label>
                            <div class="col-md-6">
                                <input type="number" class="form-control" name="mobile">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-md-4 control-label">Team</label>
                            <div class="col-md-6">
                                <input type="number" class="form-control" name="team_id">
                            </div>
                        </div>
                        <!--
                        <div class="form-group">
                            <label class="col-md-4 control-label">Role</label>
                            <div class="col-md-6">
                                <input type="text" class="form-control" name="role">
                            </div>
                        </div>
                        -->
                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <button type="submit" class="btn btn-primary">
                                    Register
                                </button>
                            </div>
                        </div>

                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

然后,我填写了表单中的所有字段,当我提交时,我会收到用户名,密码,电子邮件。我没有收到其他字段&#34;手机号码&#34;和&#34; team_id&#34;。(团队ID是我在表单中输入的内容) fields- username,password is saved but mobile and team aren't

我可以使用控制器创建它,定义我自己的路由并手动完成。但是,我不想这样做。我想在laravel 5.1中使用默认用于身份验证的相同用户模型。 我需要的是:::是否有任何方法可以使用默认情况下迁移中存在的users表在数据库中创建和存储这些列。

我将移动设备从整数更改为字符串并尝试但无法将值输入数据库。我想输入的值存储在我的用户表中。任何人都可以帮我解决我的问题。

我正在使用bestmomo / scaffold包。

1 个答案:

答案 0 :(得分:0)

public function up(){//}就像模式构建器一样,它只会使用默认值(如果已指定但是不是实际数据)向表中添加列。
如果要将数据添加到数据库中。
/auth/register路由到AuthController App\Http\Controllers\Auth的路线图 您必须覆盖postRegister中的方法AuthController 这是它的样子

<?php namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use App\User;
use Illuminate\Http\Request;
use Hash;

class AuthController extends Controller {


    public function postRegister(Request $request){

        $user = new User();
        $user->name = ucwords($request->input('name'));
        $user->email = strtolower($request->input('email'));
        $user->phone = $request->input('phone');
        $user->address = $request->input('address');
        $user->password = Hash::make($request->input('password'));

        $user->save();

        return redirect('/donner');
    }


}

并在routes.php

Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);