【发布时间】:2019-01-26 11:11:29
【问题描述】:
我目前在项目的概念部分遇到问题。 我读了很多帖子,没有人中止这个问题。 我使用 Laravel 5.6 作为我的网络应用程序的 API。
所以我有两种类型的用户:
- 简单用户
- 专业用户
“专业人士”有一个仪表板,他可以做很多事情,“简单用户”可以联系“专业人士”。
但“专业人士”也可以是“简单用户”,他可以联系其他“专业人士”。这是我在模型和数据库迁移的概念上遇到的问题。我不知道如何链接它们,我不想在“专业”表中复制所有“简单用户”变量,而只是在这个表中添加变量。 (不知道是不是显式)
所以我首先创建了一个用户模型/迁移:
应用程序/用户.php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'email', 'password', 'old_password', 'last_name', 'first_name', 'birthday', 'address',
'postal_code', 'city', 'country', 'phone_number'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'id', 'password', 'old_password', 'remember_token'
];
/**
* The attributes that should be handled by Carbon
*
* @var array
*/
protected $dates = [
'created_at',
'updated_at',
'deleted_at',
'birthday'
];
}
数据库/迁移/..create_users_table.php
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('email')->unique();
$table->string('password');
$table->string('old_password');
$table->string('phone_number', 50);
$table->string('last_name');
$table->string('first_name');
$table->tinyInteger('sex');
$table->date('birthday');
$table->text('address');
$table->string('postal_code', 10);
$table->string('city', 50);
$table->string('country', 50);
$table->boolean('valid_email')->default(false);
$table->timestamps();
$table->rememberToken();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
专业模型/迁移:
App/Professional.php
命名空间应用程序;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Professional extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'profession', 'organisation', 'subscription', 'activity_start_at', 'valid_num_organisation'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'user_id', 'num_organisation'
];
/**
* The attributes that should be handled by Carbon
*
* @var array
*/
protected $dates = [
'created_at',
'updated_at',
'deleted_at',
'activity_start_at'
];
}
数据库/迁移/..create_professionals_table.php
class CreateProfessionalsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('professionals', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->primary('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->string('profession');
$table->string('organisation');
$table->string('num_organisation');
$table->string('subscription');
$table->date('activity_start_at');
$table->boolean('valid_num_organisation')->default(false);
$table->timestamps();
$table->foreign('user_id')
->references('id')->on('users')->onDelete('set null');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('professionals', function (Blueprint $table) {
$table->dropForeign('professionals_user_id_foreign');
$table->dropColumn('user_id');
});
Schema::dropIfExists('professionals');
}
}
真正的问题是,将用户和专业人士联系起来的最佳方式是什么?像“user->isOneOf()”或类似的东西。
我正在考虑为我的用户创建一个角色,如果角色为空,则用户是“简单用户”,如果不是专业用户,但我认为这不是最好的方法。
我还查看了多态链接,但我认为这不是我在这种情况下需要的。
如果您有任何想法,我正在听。
谢谢。
【问题讨论】:
标签: php database laravel model migration