How to use DB Facade for query in laravel

Facades : It provide a "static" interface to classes that are available in the application's service container.The facades serve as "static proxies" to underlying classes in the service container , providing the benefit of a terse, expressive syntax while maintaining more testability and flexibility than traditional static methods.

Laravel raw query

In this post i will show you how to use DB facade for db operations. In sometime you need to use direct query without using model class. So you can use the direct DB facade for all the insert, update,select operation in database.

You can use it by adding a namespace.

use DB;


$result = \DB::table('tablename')->get();//You have to tell PHP it is in the root namespace.
echo "<pre>";print_r($result);

Laravel insert query :


Laravel insert multiple records :


Laravel update query example:


Laravel where clause query:

DB::table('users')->where('age', '>',10)->get();

Laravel group by query:


Laravel delete query : 

DB::table('users')->where('id', '=', 1)->delete();

Laravel truncate table :


I hope you like the post . please comment below for help or suggestion.

No comments:

Post a Comment

Download the eBook of react js for beginners

ReactJS basically is an open-source JavaScript library which is used for building user interfaces specifically for single page applications...