2014-04-10 169 views
0

使用laravel 4我正在嘗試向我的數據庫添加'active'列以檢查用戶是否處於活動狀態並且未掛起,這裏是我的遷移用戶表。laravel檢查用戶是否活動?

public function up() 
{ 
    // 
    Schema::create(
     'users', 
     function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('email', 300); 
      $table->string('password', 60); 
      $table->boolean('active'); // check this one 
      $table->string('first_name', 100); 
      $table->string('last_name', 100); 
      $table->string('address', 250)->nullable(); 
      $table->string('city', 100)->nullable(); 
      $table->string('state', 2)->nullable(); 
      $table->string('zip', 5)->nullable(); 
      $table->string('phone', 10)->nullable(); 
      $table->timestamps(); 
     } 
    ); 
} 

/** 
* Reverse the migrations. 
* 
* @return void 
*/ 
public function down() 
{ 
    // 
    Schema::drop('users'); 
} 

如果我使用$ table-> boolean('active'),你能幫助我嗎? //檢查這一個

* 這是正確的方法嗎?所以我可以像這樣使用Auth類來檢查用戶是否活動? *

$is_active = Auth::user()->active; 

if (!$is_active == 1) 
    { 
echo "Account not activated"; 
    } 
+0

除了將其命名爲「活動」而不是「激活」之外,沒關係。如果您想要添加一個字段,告訴您用戶未處於活動狀態(禁用或其他),則會更容易閱讀。 –

+0

但拉拉維爾如何讀取它'活躍'或'激活'或拉拉維爾知道嗎? – user3150060

+0

或者你的意思是,如果我把它'激活',然後檢查:Auth :: user() - >激活; ? – user3150060

回答

1

你可以做到這一點,你所提到的方式,但Laravel辦法做到這一點存在

if (Auth::attempt(array('email' => $email, 'password' => $password, 'active' => 1))) 
{ 
    // The user is active, not suspended, and exists. 
} 

閱讀:Laravel\Docs\Authentication

只是我的2位:一路正在建設你不建議你的數據庫。您應該將您的用戶登錄信息(emailpassword & active)保存在users表中,並將其他人保存在單獨的user_profile表中。稍後您可以使用eloquent to map relations

+0

如果我可能會問,將登錄名和個人資料信息分離到不同表中的優點是什麼? – jeteon

+2

您將得到一個精益認證表,使查詢更快。然後,您可以根據需要使用所需列填充user_profile表。 – ultimate