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 4 Basics Laravel and Databases Eloquent ORM

bruce zhang
bruce zhang
1,317 Points

how to Try Cache exceptions for orm query?

Hi, I try to catch exception for example if I put id = 5 (not in my database) then laravel redirect me to the error page, I tried use try catch, but it still redirect me there

    try{

        $list = \TodoList::find($id);

    }catch(\Exception $e) {

        dd("something not right");

    };

    return View::make('todos.show')
    ->withList($list);

would you help please? thanks

1 Answer

Petros Sordinas
Petros Sordinas
16,181 Points

Bruce,

What is the error you are getting?

With find(), you have to check if the result is null. It won't return an error if the id is not found. You may want to use findOrFail() which throws a ModelNotFound exception with the id is not found.