SQL Raw Queries In Laravel

Mar 23, 2017 sql laravel raw query

Learn to love it. I find it much more concise than using the Eloquent that helpers and Query Builder that have been provided. Eloquent is definitely great and easy to use but I feel I don't need another syntax I need to follow as SQL queries are much more familiar to me. Additionally (I'm not aware of the full details) Eloquent is slower. So if you're planning to scale your app it's better to stick with raw queries plus they're not that hard!

To use raw queries within your Model

$myQueryResult = DB::select(DB::raw("SELECT *
FROM appointments
WHERE appointments.aptdate = '2017-03-03'));

You'll also want to include the DB Facade at the top of your file

use Illuminate\Support\Facades\DB;

Though I'm successful without the full path in my projects

use DB;

If you found the following info helpful, I'm happy to accept any donations of the following cryptocurrencies.

  • Bitcoin - 17DTiPExzP9StqveW428acEyB4mVMfKbiK
  • Ethereum - 0x87B8307FD20dc90cc05c94905Ec593134D32B6FF
  • Litecoin - LZMiz5U5sVq9doMLYE3gfLJrxCQDKuyCmU
  • Neo - AXv71WB38ajc1KUUEnxQKhynLLPc4BapVb