Advertisement

TrimwebsolutionsTrimwebsolutions

Problem and Solutions

Refactor Query Builder in Laravel ?

By M.K. Verma · 2 May 2022

Refactor Query Builder in Laravel ?

You can write a base function and pass tableName to it and execute certain action

public function getData($tableName) {
  $query = DB::table($tableName)->where('text', 'like', '%string')->get();
  foreach ($query as $row) {
          ...
   }

  // return result;
}

$tables = ['table1', 'table2', 'table3'];
$queryResponse = [];

foreach($tables as $tableName) {
    $queryResponse[$tableName] =  $this->getData($tableName);
}