<?php
namespace App\Http\Livewire;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
class SqlTable extends Component
{
public $receivedQuery = '';
public $sqlResultsJson = '';
public $sqlResults = [];
public $headers = [];
protected $listeners = ['querySubmitted' => 'handleQuerySubmitted'];
public function handleQuerySubmitted($query): void
{
$this->receivedQuery = $query;
$this->sqlResultsJson = json_encode(DB::select($query));
$this->sqlResults = json_decode($this->sqlResultsJson, true);
// Get headers from the first row, if available
if (!empty($this->sqlResults)) {
$this->headers = array_keys($this->sqlResults[0]);
} else {
$this->headers = [];
}
// Init the DataTable
$this->emit('refreshComponent');
}
public function render()
{
return view('livewire.sql-table');
}
}
<?php
namespace App\Http\Livewire;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
class SqlTable extends Component
{
public $receivedQuery = '';
public $sqlResultsJson = '';
public $sqlResults = [];
public $headers = [];
protected $listeners = ['querySubmitted' => 'handleQuerySubmitted'];
public function handleQuerySubmitted($query): void
{
$this->receivedQuery = $query;
$this->sqlResultsJson = json_encode(DB::select($query));
$this->sqlResults = json_decode($this->sqlResultsJson, true);
// Get headers from the first row, if available
if (!empty($this->sqlResults)) {
$this->headers = array_keys($this->sqlResults[0]);
} else {
$this->headers = [];
}
// Init the DataTable
$this->emit('refreshComponent');
}
public function render()
{
return view('livewire.sql-table');
}
}