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 The Database Class & Query Builder

I get an error after using return DB::table('todo_lists')->where('name', 'Your List')->first();

After modifying the code to DB::table('todo_lists')->where('name', 'Your List')->first(); , I get the following error: The Response content must be a string or object implementing __toString(), "object" given.

Can you give me a tip how to resolve this? Thank You.

3 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Bob,

For future reference, you should always include your code when posting questions, but I bet I know what the issue is anyway :).

Notice that Hampton changes the route closure to just store the result of the query in a variable, and then var_dumps the variable. If you left it as return DB..., you would be trying to cram the entire response (which is an object) into the string that Laravel would normally display on the page. Instead, if you just store the response in a variable, and then var_dump it, PHP is handing Laravel a string to output on the screen.

It's a bit confusing, but basically, make sure you aren't trying to return the entire result of the query.

// Do this:
$result = DB::table('todo_lists')->where('name', 'Your List')->first();

// NOT this:
return DB::table('todo_lists')->where('name', 'Your List')->first();

Hope this helps!

Anthony c
Anthony c
20,907 Points
Route::get('/db', function() {
    $result = DB::table('todo_lists')->where('name', 'Your List')->first();
    return $result->name;
});

This seems like this was an editing error on whoever produced the video. The part where Hampton adds the variable instead of the return statement is never shown on-screen.