laravel在一个帐户下拥有多个电子邮件地址 [英] laravel multiple email addresses under one account

查看:157
本文介绍了laravel在一个帐户下拥有多个电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的laravel应用程序要求用户可以具有多个可用于登录的电子邮件地址.

My laravel application requires that users can have multiple email addresses they can use to login.

我的问题是,如何允许用户在一个帐户下拥有多个电子邮件地址?我必须记住,每封电子邮件只能由一个用户使用.

My question is, how would one go about allowing users to have multiple email addresses under one account? I have to keep in mind that each email may only be used by one user.

我的想法是为电子邮件创建一个单独的表,其中包含用户ID.我仍然想使用Auth类,并且想知道是否有可能?

My idea was to have a separate table for emails in which I would include user ID's. I do still want to use the Auth class though, and was wondering if that would be possible?

推荐答案

您可以拥有一个用户表,该表具有一个单独的电子邮件表,并链接到该user_id.由于您使用的是Laravel,因此请像这样为电子邮件表运行迁移.

You can have a user table with a separate email table linked to the user_id. Since you are using Laravel, run a migration for the email table like this.

public function up()
{
    Schema::create('email_table', function(Blueprint $table)
    {
        $table->primary('id');
        $table->integer('user_id');
        $table->string('email')->unique();
        $table->timestamps();
    });
}

然后在您的电子邮件模型中创建一对多关系.

Then in your email model create a one to many relationship.

class Email extends Eloquent {

    public function user()
    {
        return $this->belongsTo('User');
    }

}

并更新用户模型.

class User extends Eloquent {

    public function emails()
    {
        return $this->hasMany('Email');
    }

}

在视图中向用户请求电子邮件时.

When requesting the emails from the user in a View.

// using Blade
<ul>
@foreach($user->emails as $email)
    <li>{{ $email }}</li>
@endforeach
</ul>

或者您也可以使用Auth Class.

Or you can also use the Auth Class.

Auth::user()->emails

代替$ user->电子邮件

instead of $user->emails

这篇关于laravel在一个帐户下拥有多个电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆