It seems I\'m not the only person struggling with the differences between Laravel\'s DB::raw(), DB::select(), DB::statement(), and DB::unprepared() methods. It seems as if
More Correct Answer (I Think) For Laravel >= 5.0
DB::raw()
Produces "raw" which means "non-sanitized" string. That's why on Laravel Docs there is a warning:
DB::select() , DB::insert() , DB::update() , DB::delete()
These are the practical methods that Laravel offers which run prepared statements.
DB::statement()
This is again for running prepared statements which are not specific to CRUD.
The methods which use prepared statements provide protection for SQL injection whereas this responsibility belongs to the programmer when using raw expressions.
DB::unprepared()
There is no information about it on Laravel Docs and very little information on internet. Nevertheless I have tested it considering the answer above and this:
Laravel, create MySQL trigger from Migration
e.g. I have tried to create a MySQL trigger with DB::statement() method first but it gave me this error when migrating:
Exception trace:
1 PDOException::("SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if
your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.")
C:\wamp64\www\oscared-laravel\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458
2 PDOStatement::execute()
C:\wamp64\www\oscared-laravel\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458
However, when I tested it using DB::unprepared(), it worked smoothly. (Laravel 5.8)