У меня есть таблица Task
, где она имеет creatorID
и assignedTo
.Оба они являются внешними ключами первичного ключа моей User
таблицы u_id
.Я пытаюсь отобразить имена creatorID
и assignedTo
, но он не работает, выдал ошибку Trying to get property 'name' of non-object
.Кто-нибудь может определить, что не так с моим кодом?Я открыт для любых предложений или изменений в моих моделях / таблицах, спасибо.
Таблица задач (миграция)
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();
});
Таблица пользователей (миграция))
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();
});
Модель задачи
public function users(){
return $this->belongsTo('App\User','u_id');
}
public function assignedTo(){
return $this->belongsTo('App\User','assignedTo');
}
Модель пользователя
public function tasks(){
return $this->hasMany('App\Task','u_id');
}
public function assignedTo(){
return $this->hasMany('App\Task','assignedTo');
}
Запрос
$ActiveTasks = Task::where('p_id',$projectID)->where('status','active')->get();
Файл Blade
@foreach($ActiveTasks as $task)
<p>{{$task->assignedTo->name}}</p> <<< NOT WORKING
@endforeach