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 trial

PHP

Laravel Nested Resource - show todoItem

I've taken the tutorial a bit further, and I'm now trying to show the contents of a single todo item.

I've set up a new nested resource route like this:

Route::resource('todos.items', 'TodoItemController');

And on the frontend I can visit laravel.app/todos/4/items/9 and it will show the todo item with an id of 9. However, if I change the todo list id to something else, It still shows item 9 instead of giving an error.

How would I set up my controller or model to check if the item is in the given list id?

This is the current show function of my controller:

public function show($list, $id)
    {
        $todoList = TodoList::findOrFail($list);
        $todoItem = TodoItem::findOrFail($id);

        return View::make('todos.items.show')->with([$todoList, $todoItem]);
    }
Emmanuel Salom
Emmanuel Salom
Courses Plus Student 8,320 Points

Have you created a model for TodoList and TodoItem? what I usually do is create controllers for each model that way it could be easer to track bugs...

1 Answer

public function show($list, $id){

    $todoItem = TodoItem::findOrFail($id);

    return view ("items.show");

    ->withTodoItem($todoItem)
    ->withTodolistId($list);

}