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

louischappell's avatar

changing DB::connection() to query()

*** please delete this, I'm an idiot ***

Hello, I want to change my method of searching from DB::connection(mysql2) to query() so I can use multiple dynamic filters, but not sure what I need to 'use', or what the correct syntax is. I've looked through the docs but can't find what I'm looking for.

I'm a moron. spent 6 hours searching and reading documentation yesterday only for it to click now. Nothing requires changing. If you separate the query and the get into two variables, you can drop the if filter clauses straight in between the variables.

i.e.

$query = DB::connection('mysql2')
          ->table($vehicle->manufacturer)
          ->select('manufacturer', 'name', 'model', 'price');
          if ($vehicle->name !=''){$query = $query->where('name', $vehicle->name);}; 
 $queryResults = $query->get();
0 likes
2 replies
jlrdw's avatar

A connection is just that, a connection to the database.

A query is a query.

DIg into the docs and study the examples under database and eloquent.

https://laravel.com/docs/5.7/database#using-multiple-database-connections

https://laravel.com/docs/5.7/queries

https://laravel.com/docs/5.7/pagination

https://laravel.com/docs/5.7/eloquent

https://laravel.com/docs/5.7/eloquent-relationships

And more

https://laravel.com/docs/5.7

Jeffrey has many free intro videos.

louischappell's avatar

@jlrdw, thanks for answering. Just after posting it clicked. I didn't get to read your response, but appreciate you posting the links. I read all of those docs yesterday. I don't know why my brain wasn't working. Think I gaslighted myself pretty hard. Thanks again.

Please or to participate in this conversation.