2016-08-07 48 views
0

在cakePhp 2年經驗後,最近我開始學習laravel框架。我已經創建的數據庫爲我的新的項目並使用Laravel 5創建自定義註冊並從默認身份驗證登錄

php artisan make:auth 創建一個默認的身份驗證系統與laravel 5,但我不能根據我的數據庫設計 修改它,我有一個

  • 用戶表(ID ,用戶名,密碼,remember_token)
  • user_profiles表(ID,名字,姓氏等)
  • 組表(ID,組名)
  • group_users(GROUP_ID,users_id)
  • users_user_profiles(USER_ID,user_profile_id)

所以使用上表中我想要實現基於組的用戶管理系統。

,如果我使用默認的認證系統,

  1. 我如何能保存登錄和註冊信息到兩個表中從一個註冊?以及如何可以自動插入ID數據透視表users_user_profiles表?
  2. 如何創建包含登錄詳細信息和註冊詳細信息的註冊視圖,如何將它們分開以及如何分別訪問請求數據以將其存儲到兩個單獨的表中?

如果任何人都可以提供這方面的詳細說明,這將幫助所有laravel 5個初學者,我認爲它是一個常見的問題爲所有新手.. 請幫我 感謝

+0

您是否知道Eloquent ORM? – jonju

+0

是的,我知道關於關係表 –

回答

1

先定義好你的模型,所以你可以用雄辯的,例如,使與user_profiles連接:

public function profiles() 
    { 
     return $this->belongsToMany('UserProfile'); 
    } 

(只有當你用戶配置模式將有可能) 然後通過添加FIRST_NAME擴展您的登記表,last_n AME等。在權威性控制器創建用戶後

$user = User::create([ 
     'name' => $data['name'], 
     'email' => $data['email'], 
     'password' => bcrypt($data['password']), 
    ]); 

添加代碼,創建用戶配置文件:

$userProfile = UserProfile::create([ 
     'first_name' => $data['name'], 
     'last_name' => $data['email'] 
    ]); 

然後通過attach命令將它添加到用戶(如果你有很多都可以用sync ):

$user->userProfiles()->attach($userProfile->id); 

如何用戶驗證工作,你可以找到消滅文件更多信息:\vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php

+0

如何更新數據透視表? 我有一個如上所述的group_user數據透視表,如何在自動保存用戶詳細信息後進行更新 –

+1

與$ user-> userProfiles() - > attach($ userProfile-> id)保存到users_user_profiles一樣。 – Geding

+0

我想要的是,當用戶使用註冊頁面進行註冊時,我將一個隱藏字段作爲group_id傳遞。我想在保存用戶數據時自動更新數據透視表group_user。 –