0

我无法从其他表中附加列(引用)。就我而言,我有User.php模型和UserContact.php模型。

create_user_contacts_table

Schema::create('user_contacts', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedBigInteger('user_id');
    $table->char('phone_no')->nullable();
    $table->char('telephone_no')->nullable();

    $table->foreign('user_id')
        ->references('id')
        ->on('users');
});

这就是问题所在。我想将表中的email列附加users到我的user_contacts表中。我试过UserContact::all();但它不起作用,它结束了一个错误:

超过 60 秒的最大执行时间

用户联系人.php

class UserContact extends Model
{
    //fillables

    public function user()
    {
        return $this->belongsTo('App\User');
    }

    protected $appends = ['email'];

    public function getEmailAttribute()
    {
        return $this->user->email;
    }
}

但是当我在下面尝试这些时,它正在工作。

$u = UserContact::first();
$u->user->email;

有人知道如何实现这个东西吗?

4

0 回答 0