path = $path ?? 'exports/'; $this->file = $file ?? rand().'.sql'; } /** * Execute the job. * * @return string */ public function handle() { $downloadPath = $this->path.$this->file; $user = auth()->user(); $account = $user->account; $sql = '# ************************************************************ # '.$user->first_name.' '.$user->last_name." dump of data # {$this->file} # Export date: ".now().' # ************************************************************ '.PHP_EOL; $tables = DBHelper::getTables(); // Looping over the tables foreach ($tables as $table) { $tableName = $table->table_name; if (in_array($tableName, $this->ignoredTables)) { continue; } $tableData = DB::table($tableName)->get(); // Looping over the rows foreach ($tableData as $data) { $newSQLLine = 'INSERT INTO '.$tableName.' ('; $tableValues = []; $skipLine = false; // Looping over the column names $tableColumnNames = []; foreach ($data as $columnName => $value) { array_push($tableColumnNames, $columnName); } $newSQLLine .= implode(',', $tableColumnNames).') VALUES ('; // Looping over the values foreach ($data as $columnName => $value) { if ($columnName == 'account_id' && $value !== $account->id) { $skipLine = true; break; } if (is_null($value)) { $value = 'NULL'; } elseif (! is_numeric($value)) { $value = "'".addslashes($value)."'"; } array_push($tableValues, $value); } if (! $skipLine) { $newSQLLine .= implode(',', $tableValues).');'.PHP_EOL; $sql .= $newSQLLine; } } } // Specific to `accounts` table $tableName = 'accounts'; $tableData = DB::table($tableName) ->where('id', '=', $account->id) ->get() ->toArray(); foreach ($tableData as $data) { $data = (array) $data; $values = [ $data['id'], "'".addslashes($data['api_key'])."'", $data['number_of_invitations_sent'] ?? 'NULL', ]; $newSQLLine = 'INSERT INTO '.$tableName.' (id, api_key, number_of_invitations_sent) VALUES ('; $newSQLLine .= implode(',', $values).');'.PHP_EOL; $sql .= $newSQLLine; } Storage::disk(config('filesystems.default'))->put($downloadPath, $sql); return $downloadPath; } }