laravel multiple email addresses under one account
laravel, php
Solution
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');
}
}
And update the user model.
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>
Or you can also use the Auth Class.
Auth::user()->emails
instead of $user->emails
Problem
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. 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?