【发布时间】:2018-06-24 07:49:37
【问题描述】:
我想在身份验证中集成多个表,我已经完成了这些步骤:
首先我创建了一个迁移:
php artisan make:migration createTouristsTable
在迁移文件中:
public function up()
{
Schema::create('tourists', function (Blueprint $table) {
$table->increments('id');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
运行迁移
php artisan migrate
游客模型:
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Tourists extends Authenticatable
{
use Notifiable;
protected $table = 'tourists';
protected $fillable = ['email', 'password'];
protected $hidden = ['password', 'remember_token'];
}
我已修改 config/auth.php 文件以添加我的自定义守卫“游客”。
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'tourists' => [
'driver' => 'session',
'provider' => 'tourists',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'tourists' => [
'driver' => 'eloquent',
'model' => App\Tourists::class,
],
]
现在在我的 LoginController 中,我需要指定要使用的守卫:
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function guard()
{
return Auth::guard('tourists');
}
}
现在,我如何为此表创建登录和注册表单?
【问题讨论】:
-
为什么访客需要登录?那么访客与普通用户的区别是什么?
-
我给了一个花哨的名字“客人”。我意识到这不是最好的。
-
我用游客代替了客人。
-
我建议您只为用户设置一个表...毕竟他们都是用户...并使用角色创建一个表然后将角色分配给用户...这是最好的和众所周知的方法来解决您遇到的问题。
-
在这种情况下,问题仍然存在,因为我需要按角色切换不同的注册表单。每个角色都必须有不同的字段订阅和不同的功能。
标签: php mysql laravel laravel-5 laravel-5.3