I am using Laravel v11.15.0 (PHP v8.3.9) and I want to execute a Stored Procedure on a remote server. I know that the connection to the remote server works because I tried it by calling the names of the databases (translate and comment in the code):
Route::get('/list-databases', function () {
try {
// Query to retrieve the names of all databases on the server with the specific connection
$databases = DB::connection('sqlsrv_external')->select('SELECT name FROM sys.databases WHERE name NOT IN (\'master\', \'tempdb\', \'model\', \'msdb\');');
// Returning the results as JSON
return response()->json([
'success' => true,
'databases' => $databases,
]);
} catch (\Exception $e) {
// Handling connection errors
return response()->json([
'success' => false,
'error' => $e->getMessage(),
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
});
My question is how to execute the query to this remote server using "DB::connection('sqlsrv_external')->select....... "?
This is my configuration that needs to execute a stored procedure on the remote server and write to the local (where the Laravel database is, the result)
```php
<?php
namespace App\Http\Controllers;
use Illuminate\Console\Command;
use Illuminate\Http\Request;
use App\Models\Payment; // I assume you are using the Payment model
use Illuminate\Support\Facades\DB;
use Log;
class SyncPaymentsController extends Controller
{
public function index()
{
Log::info('Starting the synchronization process.');
try {
$results = $this->syncPayments();
Log::info('Synchronization completed successfully.');
// Passing the results to the view to be displayed to the user
return view('sync-payments', ['status' => 'completed', 'results' => $results]);
} catch (\Exception $e) {
Log::error('SQL Query Failed', ['error' => $e->getMessage()]);
return view('sync-payments', ['status' => 'failed', 'error' => $e->getMessage()]);
}
}
private function syncPayments()
{
$fromDocDate = '2023-01-01 00:01:00';
$toDocDate = '2024-07-09 00:00:00';
try {
$sql = "SET NOCOUNT ON; EXEC dbo.DOCUMENT_PAYMENT_WITH_PAYMENTDATE_PROFORMA_LARAVEL @fromDocDate = '2023-01-01 00:01:00', @toDocDate = '2024-07-09 00:00:00'";
$results = DB::select($sql, [$fromDoc