Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

larabella's avatar

Using normal MySQL instead of the query builder in Laravel

Hi,

I am the next one ;-), who is going over form CI to Laravel. Looks very nice at first sight. DId some lessons "Laravel from scratch" and it looks more advanced then CI.

My anxiety with CI is that it stops in the near future.........

I believe that using the right language with the right method is the best, so my question is; Is it possible (and smart) to use the mysql language instead of the query builder ?

I noticed (for example) when using a query like so (in Laravel) $task = DB::select('select * from tasks where id = '.$id.''); (instead of: $task = DB::table('tasks')->find($id);) it passes an array. With CodeIgniter you could end a query with result() or row()......

Hope someone does know this.

Thanks

0 likes
4 replies
jlrdw's avatar

You need to learn all of these:

  • db see docs
  • querybuilder
  • orm
  • the getPdo() function

The last one getPdo() gives a PDO connection for normal regular pdo queries. Laravel is very very flexible.

Edit last one works like

$postdata = array(
                'dogid' => $dogid,
                'dogpic' => $dogpic,
                'dogname' => $dogname,
                'sex' => $sex,
                'comments' => $comments,
                'adopted' => $adopted,
                'lastedit' => $lastedit
            );

then

$sql = "UPDATE " . PREFIX . "dogs SET dogpic = :dogpic, dogname = :dogname, sex = :sex, comments = :comments, adopted = :adopted, lastedit = :lastedit WHERE dogid = :dogid";
$stmt = DB::getPdo()->prepare($sql);
$stmt->execute($postdata);

Regualr pdo and you can use PDO::FETCH_OBJ or PDO::FETCH_ASSOC whatever you want and need to do.

1 like
larabella's avatar

@jlrdw : You'll probably right, but I hope I can convert some CI projects into Laravel and therefore I hope I can use my mysql queries as the are. (Coded into raw mysql).

jlrdw's avatar

You can, but may require small tweaks. I converted once a java jsp/servlet application to php. If you know your code, small tweaking isn't that hard.

larabella's avatar

But the main question stays:

Can I get a single row, thus not an array with this query?

$task = DB::select('select * from tasks where id = '.$id.''); like this one:

$task = DB::table('tasks')->find($id);)

Please or to participate in this conversation.