Laravel and Kint Debugging

Laravel

So, a while ago I wrote this post about working with Laravel and Kint. It works, but it can be a pain to manage through composer updates (losing your lock file, whatever happens), Laravel upgrades, it doesn't matter. The problem is that sometimes using the dd() function doesn't always display the really nice Kint table breakdown when using it with Laravel. Let's fix that once and for all!

Requiring Kint

Since we are trying to get everything to work nicely with Kint, we should probably have Kint required in our project. Add this to your composer.json file (either in your require or require-dev section. I put mine in the require-dev section):

...
"raveren/kint": "0.9.*",
...

The Function

Looking through the issue some more today, I decided to write a new modify one of Kint's functions to work really nice with Laravel. Add this function to some helpers file, or a file that is accessed throughout your application:

if (!function_exists('lk'))
{
    /**
     * Dump information about variables that Laravel will like.
     *
     * @param mixed $data
     *
     * @return void|string
     */
    function lk()
    {
        if ( !Kint::enabled() ) return null;

        $args = func_get_args();
        call_user_func_array( array( 'Kint', 'dump' ), $args );
        die;
    }
}

This is almost the same function as Kint's d() function, except that I've omitted the return statement. It doesn't seem to work every time with the return line in. We can use the new lk() (Laravel Kint) function just like we used the dd() function:

lk(Input::all());

Conclusion

There we go! A little function (lk()) that I can use without fuss to get the niceness of Kint when using Laravel. Not a complicated solution, but those are the best. Any questions, comments? Leave a comment down below. Liked it? Feel free to share it, and explore the site a little bit!

Start the Discussion

Leave a comment

Subscribe to our newsletter