laravel中的3列引用同一张表

时间:2019-03-07 14:32:57

标签: laravel migration

我正在将一个项目与一个仅基于应用程序的电子邮件系统放在一起。尝试做一些我不是100%确定的事情可以做到,但不知道解决方法。基本上,有没有一种方法可以让一个表中的3列从数据库中其他位置的同一表中提取数据?即:

public function up()
{
    Schema::create('email', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->text('body');
        $table->string('user_id'); //for the 'to' field column
        $table->string('user_id'); //for the 'from' field column
        $table->string('user_id'); //for the 'Cc' field column
        $table->timestamps();
    });
}

希望我的解释有意义。显然,user_id引用了用户表

3 个答案:

答案 0 :(得分:1)

执行此操作的最佳方法是为每个列赋予唯一的名称:

public function up()
{
    Schema::create('email', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->text('body');
        $table->string('user_to_id'); //for the 'to' field column
        $table->string('user_from_id'); //for the 'from' field column
        $table->string('user_cc_id'); //for the 'Cc' field column
        $table->timestamps();
    });
}

然后,当您发送一封新电子邮件时,在EmailController.php中,您将使用类似以下内容:

public function store() 
{
    $email = new Email; // Email model
    $email->user_to_id = $request->recipient_input
    $email->user_from_id = $request->cc_input
    $email->user_cc_id = Auth::user()->id; // Guessing that the from id is from a logged in person
    $email->title = $request->title;
    $email->body = $request->body;
    $email->save();
}

类似的东西:)

答案 1 :(得分:0)

可能的,但是您必须为列指定不同的名称:

public function up()
{
    Schema::create('email', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->text('body');
        $table->string('user_to_id'); //for the 'to' field column
        $table->string('user_from_id'); //for the 'from' field column
        $table->string('user_cc_id'); //for the 'Cc' field column
        $table->timestamps();
    });
}

(我假设您的用户表具有字符串主键,并且故意将字符串作为字符串创建外键)

答案 2 :(得分:0)

您可以为这些列指定不同的名称,然后指定具有外键约束的引用。

此外,由于id表中的users主键可能是increments('id'),因此您应将user_id_*列设为unsignedInteger


迁移

public function up()
{
    Schema::create('email', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->text('body');
        $table->unsignedInteger('user_id_to');
        $table->unsignedInteger('user_id_from');
        $table->unsignedInteger('user_id_cc');
        $table->timestamps();

        $table->foreign('user_id_to')
            ->references('id')->on('users');
        $table->foreign('user_id_from')
            ->references('id')->on('users');
        $table->foreign('user_id_cc')
            ->references('id')->on('users');
    });
}

电子邮件模型

class Email extends Model
{
    protected $table = 'email';

    public function to()
    {
        return $this->hasOne('App\User', 'user_id_to');
    }

    public function from()
    {
        return $this->hasOne('App\User', 'user_id_from');
    }

    public function cc()
    {
        return $this->hasOne('App\User', 'user_id_cc');
    }
}

请参见Laravel Database Migration文档。


这可能超出了问题的范围,但是您可以做一些改进:

  • 可以抄送1个以上的用户

  • 邮件正文通常也可以为空,因此可以添加->nullable()

  • 与您的CC相同,除非您的要求如此,否则您的CC应该为可空。

相关问题