Get column value for a row from another table using id
I have a tasks table and a statuses table. The task table contains columns like id, name and status_id. I have seeded the statuses table with three rows: id: 1, name: open, id: 2, name: resolved and id: 3, name: closed. On the index action of my TaskController, I am outputting the tasks. I want to be able to get the name of the status, not just the status_id but I'm not sure what relationship I should use, as there is only three rows in the table. Or am I approaching this wrong?
@bencarter78 - Am I right in saying that with a hasOne relationship, each task will have a row in the statuses table?
I added this to my Task model:
public function status()
{
return $this->hasOne('App\Status');
}
But when I try App\Task::find(1)->status()->get(); I get an error:
BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::status()'
The way I imagined it would work is I have a status_id column on the tasks table. This references the id column on the statuses table. Which there will only ever have three rows (open, resolved, closed).