【问题标题】:How can I give two different types of users the same functionality in Laravel?如何在 Laravel 中为两种不同类型的用户提供相同的功能?
【发布时间】:2019-11-01 18:46:38
【问题描述】:

我想让两种不同类型的用户能够创建相同的事件,但是该事件需要一个 user_id,这两种不同类型的用户可以共享。

我正在 Laravel 中制作一个应用程序,其中两种不同类型的用户,玩家和供应商,每个人都可以制作一个事件。

播放器

Schema::create('players', function (Blueprint $table) {
            $table->increments('id');
            $table->string('first_name');
            $table->string('last_name');
            $table->integer('player_id_type')->default('1');
            $table->unsignedBigInteger('player_id');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });

供应商

Schema::create('vendors', function (Blueprint $table) {
            $table->increments('id');
            $table->string('store name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });

事件

Schema::create('events', function (Blueprint $table) {
            $table->increments('id');
            $table->string('user_id');
            $table->string('name')
        });

问题是玩家和供应商的 id = 1。此外,在创建事件时,我不知道是寻找具有该 id 的玩家还是供应商。

请就我如何解决这个问题或以不同的方式实施它提出一些建议。

谢谢。

【问题讨论】:

  • 我可能会创建一个包含所有用户的“用户”表,并且只有一列说明它是什么类型的用户。
  • “问题是玩家和供应商的 id = 1”。要区分使用 Vendor 或 Player 的 id,您可以在查询语句中使用别名,例如:select("*","playertable.id as pID, vendortable.id as vID")
  • 查看这篇文章。我认为你可以从中得到一些东西。 stackoverflow.com/questions/56642293/…

标签: php database laravel database-migration


【解决方案1】:

存在冲突,您在检索“事件”实体时有两个具有相同主键的表,而不是哪个表引用 user_id ?因此您可以添加第三个名为 user 的表,而不是与事件关联,从而减少冲突

【讨论】:

  • 这里不需要添加另一个表,因为使用sql别名可以解决问题。
猜你喜欢
  • 1970-01-01
  • 2014-12-09
  • 2018-04-25
  • 2020-01-29
  • 2019-09-24
  • 1970-01-01
  • 2020-06-24
  • 2010-10-29
  • 1970-01-01
相关资源
最近更新 更多