如何在Laravel中使用两个foreignId

我有两个表,如:

用户:

Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('loginid')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });

IP:

Schema::create('i_p_s', function (Blueprint $table) {
        $table->id();
        $table->string('address')->unique();

        $table->foreignId('user_id')->nullable();
        $table->string('hostname');
        $table->string('description')->nullable();
        $table->timestamps();

        $table->index('user_id');
    });

IP型号:

public function User() {
    return $this->belongsTo(User::class);
}

用户模型:

public function IPs() {
    return $this->hasMany(IP::class);
}

The user_id column means this IP is using by which user.

And now I want to add a new column last_modified which means who is the last editor of this row.

So I think the last_modified should be $table->foreignId('user_id')->nullable(); too.

但是如何在IP模型中定义关系呢?

Additionally, I call the user_id like this now.

$ips = IP::with('user')->get();

@foreach ($ips as $ip)
    {{ $ip->user }}
@endforeach

So how can I call the last_modified after the definition?

非常感谢

评论