【问题标题】:Laravel database migrattion user could be from another typeLaravel 数据库迁移用户可能来自其他类型
【发布时间】:2019-01-26 11:11:29
【问题描述】:

我目前在项目的概念部分遇到问题。 我读了很多帖子,没有人中止这个问题。 我使用 Laravel 5.6 作为我的网络应用程序的 API。

所以我有两种类型的用户:

  1. 简单用户
  2. 专业用户

“专业人士”有一个仪表板,他可以做很多事情,“简单用户”可以联系“专业人士”。

但“专业人士”也可以是“简单用户”,他可以联系其他“专业人士”。这是我在模型和数据库迁移的概念上遇到的问题。我不知道如何链接它们,我不想在“专业”表中复制所有“简单用户”变量,而只是在这个表中添加变量。 (不知道是不是显式)

所以我首先创建了一个用户模型/迁移:

应用程序/用户.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


    【解决方案1】:

    首先,感谢您对问题的详细描述。不是每个人都这样做。

    当然,这个答案大部分都是基于意见的。有许多不同的方法可以实现这种行为。但我认为应该讨论这个问题的“正确”实现,因为有很多开发人员真的不知道从哪里开始讨论这个话题。我很乐意接受任何反馈或对我的回答进行补充以使其更完整。

    所以。大多数时候,当我必须处理同一实体的多个类型/变体时,我只使用 1 个表。特别是在处理身份验证时。因为这使得编写 Laravel 应用程序变得容易得多。

    仅针对 2 种不同类型的实体。 (在您的情况下是普通用户和专业用户),我只是在 users 表上添加了一个 is_professional 布尔标志。那就足够了。

    https://laracasts.com/discuss/channels/general-discussion/laravel-auth-multi-user-type

    当您想使用更多不同的变体时,我会在表格中添加一个 role 列。如果您需要存储角色特定的数据,这可能是一个 role_id 整数来引用单独的 roles 表。但大多数情况下,您只需使用varchar 角色密钥就可以逃脱惩罚。您也可以为此使用enum,但我从不这样做。以后迁移表时,我总是遇到问题。不确定他们是否已经解决了这些问题。

    在您的 users 表中存储仅针对某些类型的用户填充的额外数据并不可耻。例如,在您的情况下,这将是 professionorganisation 列。在某些情况下接受非规范化。这完全取决于数据的大小和使用情况。

    http://www.vertabelo.com/blog/technical-articles/denormalization-when-why-and-how

    如果在您的应用程序中不适合非规范化。在您的情况下,您还可以为 professional_users 的任何其他信息创建一个单独的表。你应该编写一些逻辑来确保你可以为你的模型加载额外的信息。也许在boot 方法中。或者可能是scope

    如果你愿意。您还可以为两个用户创建完全不同的身份验证系统。登录时分离逻辑。但是当你这样做时,你必须选择哪个用户可以访问某个页面。我认为这两种类型的用户都很难实现可访问性。

    https://scotch.io/@sukelali/how-to-create-multi-table-authentication-in-laravel

    就像我说的,对于您的问题没有通用的理想实现。要特别回答您的问题,我必须从上到下了解您的应用程序。只有开发人员,你;),才能形成最优化的策略。也许这不是您要寻找的答案,但这是我能给出的最佳答案。

    【讨论】:

    • 嗯,首先感谢您非常详细的回答,所有这些链接都非常有帮助。在与团队讨论后,我们都同意继续使用is_professional 解决方案。我还将查看professional_user 解决方案,看看在这种情况下如何处理范围(我不知道它是如何工作的)。我认为这是一个很好的讨论主题,如果有其他观点可能会很好。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    • 2015-06-23
    • 2020-10-02
    • 2018-04-04
    • 2017-01-26
    • 2014-06-05
    • 2016-05-16
    相关资源
    最近更新 更多