【问题标题】:laravel check if user is active?laravel 检查用户是否处于活动状态?
【发布时间】:2014-05-24 19:46:01
【问题描述】:

使用 laravel 4 我正在尝试将“活动”列添加到我的数据库中以检查用户是否处于活动状态且未挂起,这是我的迁移用户表。

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";
   }

【问题讨论】:

  • 除了将其命名为“活动”而不是“激活”之外,没关系。如果您想添加一个字段,告诉您用户不活跃(禁用或其他),它会更容易阅读。
  • 但是 laravel 如何读取它是“活动的”或“激活的”,或者 laravel 会知道它并不重要?
  • 或者你的意思是如果我让它'激活'然后检查: Auth::user()->activated; ?
  • 我的意思是,如果您想要激活字段(例如单击的电子邮件链接)和激活字段(即未禁用/暂停),它对您来说更具可读性,对于 laravel 没有区别。当然,您在 user() 上调用它就像您在表中命名它一样:)
  • 这对我来说是正确的。你遇到了什么问题?

标签: php laravel laravel-4


【解决方案1】:

你可以按照你提到的方式来做,但是 Laravel 的方式是存在的

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

阅读:Laravel\Docs\Authentication

只是我的 2 位:不建议使用构建数据库的方式。您应该将您的用户登录信息(emailpasswordactive)保存在 users 表中,并将其他信息保存在单独的 user_profile 表中。稍后您可以使用eloquent to map relations

【讨论】:

  • 如果我可能会问,将登录信息和个人资料信息分成不同的表有什么好处?
  • 您将获得一个精简的身份验证表,从而加快查询速度。然后,您可以根据需要使用所需的列来膨胀您的 user_profile 表。
猜你喜欢
  • 2017-12-30
  • 2012-06-02
  • 1970-01-01
  • 1970-01-01
  • 2016-04-26
  • 1970-01-01
  • 2019-03-18
  • 2020-01-11
  • 2019-12-10
相关资源
最近更新 更多