Sredny M Casanova
Sredny M Casanova

Reputation: 5053

DB::select failing in Laravel 5.2

I have a Laravel 5.2.45 application, also, I have a complex query to do, so I tried to consult it using:

DB::select("the query");

I read that this should work, but it's not the case, so just testing I simplified the query to: "Select * from aTable" but it also doesn't give any result, it takes a long long timeloading the webpage and then just doesnt show anything. I'm using this exactly: dd(DB::select("SELECT * FROM myTable AS mt"))

So, I'm wondering what is exaclty happening, it's still a valid function in Laravel 5.2? it's a really simple query and am not sure in what is failing. Thanks in advance!

Upvotes: 1

Views: 495

Answers (3)

Robin Hossain
Robin Hossain

Reputation: 705

I think you are trying to execute raw query. If you execute raw query in laravel please try this way:

$tableData = DB::select( DB::raw("SELECT * FROM table WHERE id = 100 ") ); dd($tableData);

You can set custom function also for printing data in your helper function like

function pr($var){ echo "<pre>"; print_r($var); echo "</pre>"; }

than you can call pr($tableData);

I think this should work for you. Thank You :)

Upvotes: 1

Parth Vora
Parth Vora

Reputation: 4114

Try this:

$users = DB::table('myTable')->get();

Upvotes: 0

M Arfan
M Arfan

Reputation: 4575

It's strange but you can check your Query Log to find problem

     $users = DB::select('SELECT * FROM myTable AS mt');
     print_r(DB::enableQueryLog());

How to enable query log

https://github.com/laravel/framework/commit/e0abfe5c49d225567cb4dfd56df9ef05cc297448

Upvotes: 0

Related Questions