laracasts_asker's avatar

PHP: how to switch from raw SQL query to laravel raw query?

When switching to Laravel, SQL queries are changed.

From this:

$sql = "SELECT * FROM table"; $result = mysqli_query($db, $sql);

to:

$sql = 'SELECT * FROM table'; $result = DB::select($sql);

(I want to use SQL raw queries in LARAVEL to make rewriting the code easier https://laravel.com/docs/7.x/database#running-queries)

Sorry if this question is obvious, but I'm a beginner and don't understand the other answers I've found. Now what to add to the new code, so the old $result and new $result behave the same?

Because new $result is an array, right? I need to have the new $result behave like the old $result to change the code easily.

Optional: do you know which topics I need to learn to understand this better myself?

0 likes
2 replies
bugsysha's avatar

Now what to add to the new code, so the old $result and new $result behave the same?

There is no way to behave the same.

Optional: do you know which topics I need to learn to understand this better myself?

PHP is the topic and why different implementations return different stuff.

Please or to participate in this conversation.