简体   繁体   English

Laravel 5.7多个外键引用相同的表主键

[英]Laravel 5.7 Multiple foreign key referencing to same table primary key

I have a table Task where it has creatorID and assignedTo . 我有一个表Task那里设有creatorIDassignedTo Both of them are foreign keys to my User table's primary key u_id . 它们都是User表主键u_id外键。 I'm trying to display the names of the creatorID and assignedTo but it is not working, it gave an error Trying to get property 'name' of non-object . 我想显示的名称creatorIDassignedTo ,但它不能正常工作,它给了一个错误Trying to get property 'name' of non-object Anyone able to spot whats wrong with my code? 有人能发现我的代码有什么问题吗? I'm opened to any suggestions or modifications to my models/tables thank you. 我愿意接受任何对我的模型/表的建议或修改,谢谢。

Task Table(Migration) 任务表(迁移)

Schema::create('tasks', function (Blueprint $table) {
    $table->increments('t_id');
    $table->string('t_name');
    $table->date('start_date');
    $table->date('end_date');
    $table->string('status');
    $table->unsignedInteger('creatorID');
    $table->unsignedInteger('assignedTo')->nullable();
    $table->unsignedInteger('p_id');
    $table->foreign('creatorID')->references('u_id')->on('users');
    $table->foreign('assignedTo')->references('u_id')->on('users');
    $table->foreign('p_id')->references('p_id')->on('projects');
    $table->string('priority');
    $table->timestamps();
});

User Table(Migration) 用户表(迁移)

Schema::create('users', function (Blueprint $table) {
    $table->increments('u_id');
    $table->string('name');
    $table->string('email')->unique();
    $table->string('password');
    $table->rememberToken();
    $table->string('status');
    $table->string('picture');
    $table->timestamps();
});

Task Model 任务模型

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

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

User Model 用户模型

public function tasks(){
    return $this->hasMany('App\Task','u_id');
}

public function assignedTo(){
    return $this->hasMany('App\Task','assignedTo');
}

Query 询问

$ActiveTasks = Task::where('p_id',$projectID)->where('status','active')->get();

Blade File 刀片文件

@foreach($ActiveTasks as $task)
    <p>{{$task->assignedTo->name}}</p>   <<< NOT WORKING
@endforeach

You may need to update the assignedTo relationship in your Task model so that it knows to reference u_id instead of id : 您可能需要更新Task模型中的assignedTo关系,以便它知道引用u_id而不是id

public function assignedTo()
{
    return $this->belongsTo(\App\User::class, 'assignedTo', 'u_id');
}

According to the docs: 根据文档:

If your parent model does not use id as its primary key, or you wish to join the child model to a different column, you may pass a third argument to the belongsTo method specifying your parent table's custom key 如果您的父模型不使用id作为其主键,或者您希望将子模型连接到其他列,则可以将第三个参数传递给belongsTo方法,以指定父表的自定义键

https://laravel.com/docs/5.7/eloquent-relationships#defining-relationships https://laravel.com/docs/5.7/eloquent-relationships#defining-relationships

You should also check the tasks table to see if the assignedTo column is null for any of the saved records. 您还应该检查任务表,以查看任何保存的记录的assignedTo列是否为空。 Since you have the nullable attribute set, any records that had a null value in assignedTo would also produce the Trying to get property 'name' of non-object error when the relationship was accessed. 由于设置了nullablenullable属性,因此在访问关系时,在assignedTo具有空值的所有记录也会产生Trying to get property 'name' of non-object错误的Trying to get property 'name' of non-objectTrying to get property 'name' of non-object If that's the case, you can define the relationship using the withDefault() method: 如果是这种情况,可以使用withDefault()方法定义关系:

The belongsTo relationship allows you to define a default model that will be returned if the given relationship is null. belongsTo关系允许您定义默认模型,如果给定关系为null,则将返回该默认模型。 This pattern is often referred to as the Null Object pattern and can help remove conditional checks in your code 此模式通常称为Null对象模式,可以帮助删除代码中的条件检查

https://laravel.com/docs/5.7/eloquent-relationships#inserting-and-updating-related-models https://laravel.com/docs/5.7/eloquent-relationships#inserting-and-updating-related-models

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM