How to use raw queries in Laravel

Laravel is an awesome framework. It provides you flexibilities for development. Once you realize it’s cool features, you will just love the framework. One such cool feature us using raw queries. You might wonder what’s new here. But this is really helpful when you want to use raw expressions like aggregate functions. The queries can be executed using DB::Raw method. Let’s have a look at the following example where we want to find out the number of users grouped by their status.

$users = DB::table('users')
            ->select(DB::raw('count(*) as user_count, status'))
            ->where('status', '', 1)
            ->groupBy('status')
            ->get();

These expressions will be injected into the query as strings, so be careful not to create any SQL injection points!

Read More : https://chittapattnaik.wordpress.com/2014/09/23/how-to-use-raw-queries-in-laravel/

Leave a Reply

Your email address will not be published. Required fields are marked *