Laravel comes with a lot of helper functions to interact with Database efficiently . There are Eloquent which composes an ORM model, and helps to write expressive relations and queries. But some cases that’s not enough, we have to have some raw queies need to write to achieve the results. So in this article we are discussing difference between Laravel’s raw SQL functions.
DB::raw()
This generates a raw query which means “non-sanitized” string. Laravel wrote a beautiful warning message in the docs, as it might cause security issues if don’t use it wisely.
DB::select()
This method is intended for simple selects.
DB::select(DB::raw('select * from customers'));
DB::statement()
This method is very useful when running queires other than CRUD, like alter a table field or disable forieng key checks etc.
DB::statement('SET FOREIGN_KEY_CHECKS=0');
DB::statement('ALTER TABLE THIS FIELD');
DB::unprepared()
This will help you to run a query that cannot be run prepared. This is not documented in Laravel docs, but we can see it declared in the code.
- Just want to thank us? Buy us a Coffee
- May be another day? Shop on Amazon using our links.
Your prices won't change but we get a small commission.