我有这样的用户和项目迁移:
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->bigInteger('last_used_society_id');
$table->bigInteger('last_used_project_id');
$table->rememberToken();
$table->timestamps();
});和
Schema::create('projects', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('eplan_name')->nullable();
$table->bigInteger('society_id');
$table->timestamps();
//$table->foreign('user_id')->references('id')->on('users');
});
Schema::table('projects', function (Blueprint $table) {
$table->foreign('society_id')->references('id')->on('societies');
});表Users在之后被修改为添加外国人:
Schema::table('users', function (Blueprint $table) {
$table->foreign('last_used_society_id')->references('id')->on('societies');
$table->foreign('last_used_project_id')->references('id')->on('projects');
});在用户模型中,我有这样的设置:
public function ActualProject(){
return $this->belongsTo('App\Models\Project', 'users_last_used_project_id_foreign');
}
public function ActualSociety(){
return $this->belongsTo('App\Models\Society', 'users_last_used_society_id_foreign');
}但是当我尝试调用$user->ActualProject时,它返回null
发布于 2020-07-30 00:41:35
为什么使用'users_last_used_project_id_foreign'?
只需按原样使用列名...
public function ActualProject(){
return $this->belongsTo('App\Models\Project', 'last_used_project_id');
}https://stackoverflow.com/questions/63158412
复制相似问题