Laravel 新手
链接表时foreignId()和unsignedBigInteger()有什么区别
$table->unsignedBigInteger('user_id');
$table->foreignId('user_id');
Run Code Online (Sandbox Code Playgroud)
我已经尝试了两者,它们都有效。
根据文档,它说:
该
foreignId方法是一个别名unsignedBigInteger
但别名是什么意思?这是否意味着它们是相同的?
PS:我没有使用文档中的代码,而只是
$table->unsignedBigInteger('user_id');
Run Code Online (Sandbox Code Playgroud)
和/或
$table->foreignId('user_id');
Run Code Online (Sandbox Code Playgroud) 我有两个表,例如:
用户:
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('loginid')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)
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');
});
Run Code Online (Sandbox Code Playgroud)
IP型号:
public function User() {
return $this->belongsTo(User::class);
}
Run Code Online (Sandbox Code Playgroud)
用户型号:
public function IPs() {
return $this->hasMany(IP::class);
}
Run Code Online (Sandbox Code Playgroud)
该user_id列表示该 IP 正在由哪个用户使用。
现在我想添加一个新列last_modified,这意味着谁是该行的最后一个编辑者。
所以我认为也last_modified应该如此$table->foreignId('user_id')->nullable();。
但如何定义IP模型中的关系呢?
user_id另外,我现在这样称呼它。
$ips = IP::with('user')->get();
@foreach ($ips as $ip)
{{ $ip->user }}
@endforeach
Run Code Online (Sandbox Code Playgroud)
last_modified那么如何在定义之后调用呢?
多谢