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 Todos

in the laravel videos hampton uses TodoList::all(); and adds it to a variable so he can extract data

eg. $todos->id

But what is this:

TodoList::all();

Referencing in here

public function index()
    {
        $todo_lists = TodoList::all();
        return View::make('todos.index')->with('todo_lists', $todo_lists);
    }

2 Answers

Aaron Graham
Aaron Graham
18,033 Points

TodoList::all() is referencing the all() method of the TodoList class. You get this by extending Eloquent, Laravel's ORM.

class TodoList extends Eloquent {

};

This would be in a file called TodoList.php in your models directory.

Edit: Here is a link to the Laravel docs on Eloquent

TodoList in TodoList::all() is your model/class for todo lists. You'll find it here: app/models/TodoList.php

$todo_lists is an array of TodoList objects, each object comes with all the methods (functions) and variables (properties) of TodoList.php. As mentioned above, TodoList extends eloquent - it therefore adopts additional built-in methods and properties, like ->id.

If you look underneath the all() method, it's simply a MYSQL statement retrieving all the todo lists from the database.

Aaron Graham
Aaron Graham
18,033 Points

Tom Cawthorn - You should re-post this as an answer. I would be happy to up-vote it. Very well explained.

Thanks Aaron Graham, I was only extending upon your points ;)

Thanks. So the model magically pulls all the data from the db and puts it in an object?

Aaron Graham
Aaron Graham
18,033 Points

It does seem rather magical. Basically, you are getting a lot of stuff with the, "extends Eloquent" statement. To give you an oversimplified example, you basically have something like the following:

class Eloquent {
//you don't need to build the Eloquent class,
//it's included with Laravel

  public function all() {
    //do some type of sql "SELECT * FROM table;" and return the results
  };

};

class TodoList extends Eloquent {
  //nothing else is required here.
  //the all() method is automatically pulled in when you extend Eloquent
};

//now you can use your TodoList class to get you database entries
$todo_lists = TodoList::all();

Hope that helps make some sense out of it. This is one of the primary objectives of object oriented programming; creating code that is easy to reuse and extend.