简体   繁体   中英

how can I add columns for App\User and store the data to in users table in laravel 5.1?

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();
    });
}

The above method creates user table with few additional columns.I then added fillable content in my User model as:

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

My create.blade.php view is:

@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

Then,I filled all the fields in the form and when I submit I get username,password,email.I am not getting other fields "mobile number" and "team_id".(Team Id is what I enter in the form) fields- username,password is saved but mobile and team aren't

I can create this with controller,defining my own routes and do it manually.But,I don't want to do that.I want to use the same User model that comes default for authentication in laravel 5.1. What I need is ::: Is there any way to create and store these columns in the database with the users table that exists in migration by default.

I changed the mobile from integer to string and tried but couldn't get the values into the database.I wan't the values that I enter to get stored in my users table. Can anyone help me to solve my problem.

I am using bestmomo/scaffold package.

public function up(){//} is like schema builder, it will only add columns to your table with default values if specified but not actual data.
If you want to add data to your database.
/auth/register route maps to AuthController in App\\Http\\Controllers\\Auth You have to overrride the method postRegister in AuthController Here is how it might look like

<?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');
    }


}

and in your routes.php

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM