Difference between Laravel DB::insert() and DB::table()->insert()

  • DB::insert() for raw sql queries. Example:

    DB::insert('insert into users (id, name) values (?, ?)', [1, 'Dayle']);

  • DB::table()->insert() for query builder. Example:

    DB::table('users')->insert( ['email' => '[email protected]', 'votes' => 0] );

Query builder compiles conditions to raw sql query, but I am using it because it is much more convenient.


You always try to use query builder as much as possible, it prevents SQL injection.

The Laravel query builder uses PDO parameter binding to protect your application against SQL injection attacks. There is no need to clean strings being passed as bindings

Query Builder also helps with special chars such as ', " in values. For raw statement, you need to take care of the special chars yourself.