Answer the question
In order to leave comments, you need to log in
Can't figure out Laravel relationships?
I am going through php training, now we are writing a project on laravel and something is stuck on relationships. Can't figure out how it works? Already read the dock and third-party resources and that there is no complete picture yet.
The question is. There are 3 tables task, user, status. I need an association between task=user and task=status.
Task = Status I set up and then probably more by typing. Now this is how I get the status name $task->status->name
of
the Task model
public function status()
{
return $this->belongsTo(TaskStatus::class);
}
$table->id();
$table->string('name');
$table->string('description')->nullable();
$table->integer('status_id')->default(1);
$table->integer('created_by_id');
$table->timestamps();
public function task()
{
return $this->hasMany(Task::class, 'status_id');
}
$table->id();
$table->string('name');
$table->timestamps();
public function user()
{
return $this->belongsTo(User::class);
}
public function tasks()
{
return $this->hasMany(Task::class, 'created_by_id');
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question