Skip to content

Commit

Permalink
fix: laravel11 中sqlite使用问题
Browse files Browse the repository at this point in the history
  • Loading branch information
slowlyo committed Jun 3, 2024
1 parent b425e16 commit 14a18cc
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/Services/AdminService.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,27 @@ public function primaryKey()
public function getTableColumns()
{
if (!self::$tableColumn) {
self::$tableColumn = Schema::connection($this->getModel()->getConnectionName())
->getColumnListing($this->getModel()->getTable());
try {
// laravel11: sqlite 暂时无法获取字段, 等待 laravel 适配
self::$tableColumn = Schema::connection($this->getModel()->getConnectionName())
->getColumnListing($this->getModel()->getTable());
} catch (\Throwable $e) {
self::$tableColumn = [];
}
}

return self::$tableColumn;
}

public function hasColumn($column)
{
$columns = $this->getTableColumns();

if (blank($columns)) return true;

return in_array($column, $columns);
}

public function query()
{
return $this->modelName::query();
Expand Down Expand Up @@ -230,11 +244,11 @@ public function sortColumn()
{
$updatedAtColumn = $this->getModel()->getUpdatedAtColumn();

if (in_array($updatedAtColumn, $this->getTableColumns())) {
if ($this->hasColumn($updatedAtColumn)) {
return $updatedAtColumn;
}

if (in_array($this->getModel()->getKeyName(), $this->getTableColumns())) {
if ($this->hasColumn($this->getModel()->getKeyName())) {
return $this->getModel()->getKeyName();
}

Expand Down Expand Up @@ -269,11 +283,10 @@ public function update($primaryKey, $data)
{
$this->saving($data, $primaryKey);

$columns = $this->getTableColumns();
$model = $this->query()->whereKey($primaryKey)->first();
$model = $this->query()->whereKey($primaryKey)->first();

foreach ($data as $k => $v) {
if (!in_array($k, $columns)) {
if (!$this->hasColumn($k)) {
continue;
}

Expand All @@ -300,11 +313,10 @@ public function store($data)
{
$this->saving($data);

$columns = $this->getTableColumns();
$model = $this->getModel();
$model = $this->getModel();

foreach ($data as $k => $v) {
if (!in_array($k, $columns)) {
if (!$this->hasColumn($k)) {
continue;
}

Expand Down

0 comments on commit 14a18cc

Please sign in to comment.