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

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Laravel - Trying to get property of non-object

Hello.

I am just starting the Eloquent ORM part and first step I get this error:

https://www.dropbox.com/s/zl9uz8oalg9ujpc/Schermata%202014-11-15%20alle%2018.17.03.png?dl=0

Since I am new to laravel I don't really have a clue about this error;

It points me to the routes.php file and here come the problems:

1) I think we haven't touched that lately, so it should be ok. I also have checked the previous video and my routes.php looks like it does in its last video appearance here on teamtreehouse.com.

2) If I donwload the project files to run a check against it it the routes.php is totally different (I guess it is from previous stages).

Any ideas what is going wrong here?

Ty

Vittorio

Ken Alger
Ken Alger
Treehouse Teacher

Vittorio;

Could you post your routes.php file?

Thanks,

Ken

4 Answers

Andrew Shook
Andrew Shook
31,709 Points

Vittorio, I think the problem is that DB:table isn't returning anything. Try putting:

Route::get('db', function() {

    $result = DB::table('todo_lists')->where('name', 'Your list')->first();

    dd($result);

   // return $result->name;

});

dd, or die and dump, will stop execution and print the $result to the screen. My guess is that the database call isn't returning anything and :

return $result->name;

is returning an error because the $result isn't an object or it is and doesn't have a name property. Either way, dd will show you the information and should allow you to sort things out.

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Yes Andrew, you are right, it has returned null

Thanks

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Ken,

there you go:

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/

/**
    * / = home
    * /todos - all lists
    * /todos/1 - show
    * /todos/1/edit - edit and update
    * /todos/create - create new list
    */

Route::get('/', 'TodoListController@index');
// Route::get('/todos', 'TodoListController@index');
// Route::get('/todos/{id}', 'TodoListController@show');

Route::get('db', function() {

    $result = DB::table('todo_lists')->where('name', 'Your list')->first();
    return $result->name;

});

Route::resource('todos', 'TodoListController');
Ken Alger
Ken Alger
Treehouse Teacher

Vittorio;

Are you getting the error message when you are trying to go to the projects/laravel-basics (or however you named it) site directly or are you trying to go to projects/laravel-basics/db?

Ken

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

OOps Ken Alger

That's embarrassing..

I was looking at the db url..

I have just realized the main one seems to work fine, but is it ok to proceed with that error on http://laravel.dev:8000/db?

Ken Alger
Ken Alger
Treehouse Teacher

Vittorio;

I have used Laravel a bit, and have worked through the Basics course a few times and have not had any issues.

Ken

Sebastian Jung
Sebastian Jung
6,882 Points

I had the same problem until i changed my Routings from

Route::get('/todos', 'TodoListController@index');

Route::get('/todos/{id}', 'TodoListController@show');

Route::get('/todos/create', 'TodoListController@create');

to

Route::get('/todos', 'TodoListController@index');

Route::get('/todos/create', 'TodoListController@create');

Route::get('/todos/{id}', 'TodoListController@show');

So it seems it has something to do with the order.

I am not a pro so i can not explain why this happens. Maybe someone else can?

Sebastian Jung
Sebastian Jung
6,882 Points

wwooops, even better to use is:

Route::resource('/todos', 'TodoListController');

instead of all three lines i wrote above.