Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialkevin revill
7,533 PointsWhat is the belongsTo for (laravel)?
In this task we added this method
public function todoList()
{
return $this->belongsTo('TodoList');
}
I have no idea what it does and it was not mentioned in the video. In fact I can remove this method and everything is still working....
What is it for? can we have an example?
1 Answer
Markus Schober
3,149 PointsThis method makes the relationship to a 'TodoList'. I haven't watched this course, but I guess this is a "Todo"-Model?
If this is the case, the Todo (Model) belongs to a Todo List (Model).
The inverse of this relation is the hasMany
method. In your case, this should go in the TodoListModel (Todo List has many Todos) like this:
public function todos()
{
return $this->hasMany('Todo');
}
To get all Todos for a specific Todolist you can do:
$todos = TodoList::find(1)->todos;
Check out the Laravel Documentation to get more info.
I hope this helps :)