Tag Archives: laravel 4

Laravel Application Logs and Logrotate

Depending on usage, errors, and what you’ve chosen to log, the Laravel application log(s) can really grow after a while.  With Laravel 3 and initially with Laravel 4, new log files were created every day by default, which kept them small and easy to search, but now Laravel 4 defaults to a single log file (which I prefer).

Continue reading

Simple SQL query troubleshooting in Laravel 4

A quick tip for seeing a visual representation of your Eloquent/Query Builder queries in Laravel 4, mostly taken from here:

In your App:before filter in filters.php add:

if ( in_array(Config::getEnvironment(), array('local','test')) and (Input::get('sql') !== null) )
 DB::listen(function($sql, $bindings, $time)

I’ve added the logic that first checks to make sure this can only be displayed in the local and test environments, and then if you append ‘?sql’ to the URL, you will see the raw SQL output.

There are other ways to display raw SQL in Laravel 4, including appending ‘->toSql()’ to your Eloquent statements or using a profiling package like this, but I think this is a nice, simple solution.

Laravel 4 Pagination with Column Sorting

Updated (4/24/2014) to address possible SQL injection attempt with $order and/or $sort input.

It took a little effort, but I finally got Laravel 4′s pagination class working with the ability to sort by column. Posted here so I don’t forget.


$widgets = Widget::all();
$allowed_columns = ['column1', 'column2', 'column3'];
$sort = in_array(Input::get('sort'), $allowed_columns) ? Input::get('sort') : 'delivery_date';
$order = Input::get('order') === 'asc' ? 'asc' : 'desc';
$widgets = $widgets->orderBy($sort, $order);
$widgets = $widgets->paginate(20);

//include $order and $sort when retrieving your layout/view
$this->layout->nest('content', 'widgets.index',array('widgets' => $widgets, 'sort' => $sort, 'order' => $order));


//Display the pagination
//I tried using appends() first, but could not chain multiples
{{ $events->addQuery('order',$order)->addQuery('sort', $sort)->links() }}

//Column header
{{link_to_route('widgets.index','Name', array('sort' => 'name'))}}
//Using Twitter Boostrap up/down arrows
<a href="{{route($route, array('sort' => $sort, 'order' => 'asc'))}}">
<i class="icon-chevron-up"></i>
<a href="{{route($route, array('sort' => $sort, 'order' => 'desc'))}}">
<i class="icon-chevron-down"></i>