2017-02-17 98 views
2

我試圖通過使用模態窗口和添加一些新字段來修改laravel的默認身份驗證系統。 所以,我有一個模式窗口註冊新用戶,看起來像這樣:Laravel默認身份驗證與模式

<!-- Modal Register area --> 
<div class="modal fade" id="registerModal" tabindex="-1" role="dialog" aria-labelledby="myRegisterLabel" aria-hidden="true"> 
    <div class="modal-dialog" role="document"> 
    <div class="modal-content"> 
     <form id="formRegister" class="login-form" action="{{ route('register') }}" method="POST"> 

     {{ csrf_field() }} 

     <div class="modal-header"> 
      <button type="button" class="close" data-dismiss="modal" aria-label="Close"> 
      <span aria-hidden="true">&times;</span> 
      </button> 
      <h4 class="modal-title" id="myRegisterLabel">Sign Up</h4> 
     </div> 
     <div class="modal-body"> 
      <div class="form-group"> 
      <label>First Name:</label> 
      <input type="text" name="first_name" placeholder="first name..." value="{{ old('first_name') }}" required autofocus> 
      </div> 
      <div class="form-group"> 
      <label>Last Name:</label> 
      <input type="text" name="last_name" placeholder="last name..." value="{{ old('last_name') }}" required autofocus> 
      </div> 
      <div class="form-group"> 
      <label>Email:</label> 
      <input type="email" name="email" placeholder="email..." value="{{ old('email') }}" required> 
      </div> 
      <div class="form-group"> 
      <label>Password:</label> 
      <input type="password" name="password" required> 
      </div> 
      <div class="form-group"> 
      <label>Password Confirmation:</label> 
      <input type="password" name="password_confirmation" required> 
      </div> 
      <div class="form-group"> 
      <button type="submit" class="btn btn-primary"> 
       Register 
      </button> 
      </div> 
     </div> 
     </form> 
     @if(count($errors)) 
     @foreach ($errors->all() as $error) 
      <div class="alert alert-danger"> 
      <p>{{ $error }}</p> 
      </div> 
     @endforeach 
     @endif 
    </div> 
    </div> 
</div> 
<!-- End Modal Register area --> 

然後我修改了RegisterController驗證規則和創建方法,包括這個新的領域以及遷移文件。 (所以,我在數據庫中的Fileds,一切都很好有)

RegisterController:

<?php 

namespace App\Http\Controllers\Auth; 

use App\User; 
use App\Http\Controllers\Controller; 
use Illuminate\Support\Facades\Validator; 
use Illuminate\Foundation\Auth\RegistersUsers; 
use Illuminate\Http\Request; 

class RegisterController extends Controller 
{ 
    /* 
    |-------------------------------------------------------------------------- 
    | Register Controller 
    |-------------------------------------------------------------------------- 
    | 
    | This controller handles the registration of new users as well as their 
    | validation and creation. By default this controller uses a trait to 
    | provide this functionality without requiring any additional code. 
    | 
    */ 

    use RegistersUsers; 

    /** 
    * Where to redirect users after registration. 
    * 
    * @var string 
    */ 
    protected $redirectTo = '/'; 

    /** 
    * Create a new controller instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     $this->middleware('guest'); 
    } 

    /** 
    * Get a validator for an incoming registration request. 
    * 
    * @param array $data 
    * @return \Illuminate\Contracts\Validation\Validator 
    */ 
    protected function validator(array $data) 
    { 
     return Validator::make($data, [ 
      'first_name' => 'required|max:255', 
      'last_name' => 'required|max:255', 
      'email' => 'required|email|max:255|unique:users', 
      'password' => 'required|min:6|confirmed', 
     ]); 
    } 

    /** 
    * Create a new user instance after a valid registration. 
    * 
    * @param array $data 
    * @return User 
    */ 
    protected function create(array $data) 
    { 
     return User::create([ 
      'first_name' => $data['first_name'], 
      'last_name' => $data['last_name'], 
      'email' => $data['email'], 
      'password' => bcrypt($data['password']), 
     ]); 
    } 
} 

在形式,你可以看到我張貼這種路由(「註冊」),但即使我沒有得到任何錯誤,當我提交表單,我得到重定向到/用戶沒有得到保存在數據庫.. 事情是,我不太確定這個默認身份驗證系統如何工作..

感謝您的幫助!

+0

你的字段添加到您的$可填寫財產上的模型? –

+0

是的,我做了,我忘了提及。 –

+0

因此,在您製作和創建時,您添加的新字段在哪裏? –

回答

0

我認爲提供的代碼是非常正確的。請確保在您的遷移中刪除了name字段表格users表格。

使用你的代碼我能夠註冊一個用戶與你的模態和控制器。是新鮮的Laravel 5.4,你的模態被放置在welcome.blade.php。另外css/js文件加入了&一個按鈕來顯示模式。

這是我up()遷移更新users

public function up() 
    { 
     Schema::table('users', function (Blueprint $table) { 
      $table->dropColumn('name'); 
      $table->string('first_name')->nullable(); 
      $table->string('last_name')->nullable(); 
     }); 
    } 

也更新User模型與新fillables:

protected $fillable = [ 
    'email', 'password', 'first_name', 'last_name' 
]; 
+0

嗯..我做了什麼之後我運行make:auth進行默認身份驗證,我直接進入用戶遷移並刪除了'name'字段,然後添加了我的first_name,last_name 。之後,我運行了php工匠遷移。這應該是問題嗎? –

+0

是的,你可以修改現有的遷移,如果如果沒有運行過。所以這不應該是一個問題 –

+0

好吧,我終於能夠得到這個運行,我現在感到非常愚蠢哈哈..問題是與我的模態,我忘了我把我的密碼驗證min:6,我是輸入一個4個字符的密碼。因爲模式在提交後關閉,所以我沒有注意到。感謝大家! –