-1

When I tried to replace laravel query builder with raw sql query, it displays some errors.

Query:

$users = DB::select('select * from action_buttons where action_button_id BETWEEN 98 AND 102');

On executing the above query the following error displays in a white screen. Screenshot of the error

"Whoops, looks like something went wrong."

I have added "use DB;" at the top. Confused why this error occurs.

MaartenDev
  • 5,631
  • 5
  • 21
  • 33
  • 1
    $users = DB::table('action_buttons')->whereBetween('action_button_id', [98, 102])->get(); – Casper Sep 13 '19 at 06:20
  • Did you config .env file properly and debug configuration? check your logs files – Mohammad Fanni Sep 13 '19 at 06:20
  • I don't know your DB schema and the error. However, my assumption is you're wanting to see the error displayed and I would guess your environment variable APP_DEBUG or APP_ENV is set to false or production. In a local Dev environment you want debug to be true and env to be local. – amac Sep 13 '19 at 06:22
  • Do not use sql queries directly. DB::raw() is used to make arbitrary SQL commands which aren't parsed any further by the query builder. They therefore can create a vector for attack via SQL injection. https://fideloper.com/laravel-raw-queries – Casper Sep 13 '19 at 06:22
  • As others mentioned this error doesn't look like query related error therefore please check your configurations and this might be helpful too, https://bobcares.com/blog/laravel-something-went-wrong/ – Casper Sep 13 '19 at 06:25
  • @casper. Thank you. This executes well for me now. I tried the print_r() and it shows the results. But the error still displays below the printed array. – Syam Suthan Sep 13 '19 at 07:08
  • Please add screenshot of printed array. – Casper Sep 13 '19 at 07:13
  • @ casper. I have added the screenshot link at the behind the question now. Kindly check. Also the fetched results displays a different value for the fieldname compared to phpmyadmin . The results shows [Home_screen] instead homescreen. Also the values are also different for this particular field. 0 is displayed in place of 1 while printing the array. – Syam Suthan Sep 13 '19 at 07:24
  • This is not related to query, did you generate application key ? – Casper Sep 13 '19 at 07:27
  • @ No. i did not generate. But there was one key when i downloaded the files. – Syam Suthan Sep 13 '19 at 07:28
  • Update the key using artisan: php artisan key:generate and update the APP_KEY entry in the environment file (.env). – Casper Sep 13 '19 at 07:50
  • @casper. Thank you. it was resolved. – Syam Suthan Sep 13 '19 at 09:02
  • Okay great, I've added solution as an answer. – Casper Sep 13 '19 at 09:10

1 Answers1

0

This issue is not related to query, please check your application key. Generate application key using artisan command php artisan key:generate and update APP_KEY in environment file.

For more info about: "Whoops, looks like something went wrong." https://bobcares.com/blog/laravel-something-went-wrong/

Other than that I saw another issue in your query, for best practice do not use sql queries directly.

DB::raw() is used to make arbitrary SQL commands which aren't parsed any further by the query builder. They therefore can create a vector for attack via SQL injection.

For more info: https://fideloper.com/laravel-raw-queries

Therefore update your query as shown below,

$users = DB::table('action_buttons')->whereBetween('action_button_id', [98, 102])->get();

Those changes will sort your problem.

Casper
  • 1,469
  • 10
  • 19