1

I just followed this todo list tutorial and I have a few questions.

  1. I would like to sort the objects by a key in Laravel and I just cannot figure this out. Where should I implement this? In Eloquent? In routes? In controllers? Slightly confused about this. I have found a few links going over the sort method using the usort wrapper but I have been unable to implement an object sort (todo list item by a key) successfully.

  2. Would it be more efficient to SQL sort the items? Using something like this:

    $sql = "SELECT * FROM `items` ORDER BY `items`.`time` ASC LIMIT 0, 30 ";
    

Where would I implement this? I feel like the logical place is in my item model. But I'm not sure.

I can post any relevant code, I am just not sure what to post yet, so feel free to ask. And I don't want to spam this post with unnecessary code.

halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

3

You should perform sorting in your query unless you have other reason, in case of Laravel using Eloquent you may do something like this:

$items = Item::orderBy('id')->get();

Basically, you may put this in your controller, for example:

class UserController extends BaseController {

    public function getAllUsers()
    {
        $users = User::orderBy('username')->get();
        return View::make('users.index')->with('users', $users);
    }

}

To make query into users table using Eloquent you have to create a User model like this:

class User extends Eloquent {
    protected $table = 'users';
}

You can also use this for same thing using the Query Builder even without a User model:

public function getAllUsers()
{
    $users = DB::table('users')->orderBy('username')->get();
    return View::make('users.index')->with('users', $users);
}

This way, you will be able to query your User model from your controller and then pass the query result to the view where you may show them. This is just a basic implementation but there are other ways using repository classes and known as best practice in a Laravel application. This is a broad topic but you may check this article.

If you have a route like this:

Route::get('users/all', 'UserController@getAllUsers');

Then the getAllUsers() method of the class declared above will be invoked to show all users. Also read the documentation, it's the best place for any reference of Laravel framework.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • Everyones input here helped. Your suggestions got me on the right track, but did not account for my authenticated sessions. I started to test my code logic using trace statements and good old fashion "guess and checking" What I ended up doing was adding this to my home('/') controller. https://gist.github.com/anonymous/11133373#file-gistfile1-txt – user3555006 Apr 21 '14 at 05:45
1

Routes are for routing so not there. Controllers are for business logic so it is a good place to process the query results. In your case I would simply create a route for a controller method and in that function, I would order with Eloquent's orderBy function, like presented in this question:

$posts = Post::orderBy('id', 'DESC')->get();

It's unnecesary to write SQL queries for something as simple as this, especially if you want to write raw queries. The reason why we use the Eloquent ORM is that we don't want to write raw queries.

You should leave the sorting to the database. Actually, you should leave all database related task to the database, it is optimized for that, and Laravel will use these features so use the framework's solutions.

Community
  • 1
  • 1
totymedli
  • 29,531
  • 22
  • 131
  • 165