1

The pagination documentation has good info on how to add sorting to the paginated links for your data:

<?php echo $orders->appends(array('sort' => 'name', 'sort_dir'=>'asc'))->links();

to generate links like this:

http://example.com/something?page=2&sort=name&sort_dir=asc

So now you have a table with headers and values and a series of paginated links you can click. Let's say I wanted to change the headers to be clickable links that allowed you to sort by that header. How would I generate that URL?

For example, lets say our current URL is what I said above:

http://example.com/something?page=2&sort=name&sort_dir=asc

And I wanted to click the "NAME" table header in order to change the sorting direction. The URL I would want is:

http://example.com/something?page=2&sort=name&sort_dir=desc

I have a controller called : AdminController.php this is the method used:

    public function getAdmins() {

 // CACHE SORTING INPUTS
$allowed = array('first_name', 'last_name', 'email', 'activated', 'crated_at'); // add allowable columns to search on
$sort = in_array(Input::get('sort'), $allowed) ? Input::get('sort') : 'first_name'; // if user type in the url a column that doesnt exist app will default to first_name
$order = Input::get('order') === 'asc' ? 'asc' : 'desc'; // default desc
$action = null;
// select all admins Group = 1
$admins = DB::table('users')
     ->join('users_roles', 'users.id', '=', 'users_roles.user_id')
     ->where('users_roles.role_id', '=' ,0)
    ->orderBy($sort, $order)
    ->paginate($this->perpage);

// check for actions
if (!is_null(Input::get('action'))) 
    {

            $action = Input::get('action');

            if ($action == "add")
            {
                $this->layout->content = View::make('admin.admins-add');
            }

    }
    else
    {

        // get current counter admin counts
        $counter = $admins->getFrom();
        View::share('counter', $counter);
        View::share('sort', $sort);
        View::share('order', $order);

        // share admin with template
        View::share('admins', $admins);

        $this->layout->content = View::make('admin.admins');
    }
    }

1). how could I display the columns headers links in Laravel inside my view page?

Thanks

6
  • use javascript ofcourse.... this has nothing to do with server side. Commented Apr 14, 2014 at 19:16
  • I need to generate header links , when you click on first name , it will change sort = first_name and so on Commented Apr 14, 2014 at 19:18
  • i got what you are trying to do here.... but this is client side coding. not server side. what did you do in client side to achieve it? Commented Apr 14, 2014 at 19:20
  • in the client side I have a table with headers first name last name email etc, I want to make those links , where if you click on first_name it will take you to : example.com/… and Last Name header will go to : example.com/… , and for the sort_dir to change if it was asc it will change to desc Commented Apr 14, 2014 at 19:23
  • Of course you can do it server side... check the answer Commented Apr 14, 2014 at 19:28

1 Answer 1

0

This is how you get the params:

$params = Request::except(['sort','sort_dir']);
$sort_dir = (Request::get('sort_dir') == 'asc') ? 'desc' : 'asc';
$sort = 'whataverYourColumnIs';

$attributes = array_merge(['sort' => $sort, 'sort_dir' => $sort_dir], $params);

// and for the link for example:
link_to_action('AdminsController@getIndex', 'columnName', $attributes);
Sign up to request clarification or add additional context in comments.

18 Comments

whats 'something.index' , in my case what is the link to my route? 'admins'??
Whatever your route is named like, might be admins.index for example.
well I have Route::controller('admin', 'AdminController');
and there is a method inside AdminController : getAdmins() , and the URL admin/admins
I tried 'admin.admins' I got an error undefined route?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.