From 16bed371c70d930f46a24ea1dcb383d5b0d396be Mon Sep 17 00:00:00 2001 From: Ramiz Kongulov Date: Thu, 17 Nov 2022 11:12:41 +0400 Subject: [PATCH] Add database sorting --- README.md | 4 +++- config/tab-translatable.php | 4 +++- src/NovaTabTranslatable.php | 7 ++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5d4a97a..e819302 100644 --- a/README.md +++ b/README.md @@ -74,8 +74,10 @@ return [ * And choose the 'code_field' for example "en", "fr", "es"... */ 'database' => [ - 'model' => 'App\\Language', + 'model' => 'App\\Models\\Language', 'code_field' => 'lang', + 'sort_by' => 'id', + 'sort_direction' => 'asc' ], /* diff --git a/config/tab-translatable.php b/config/tab-translatable.php index f60b1b9..0b82a86 100644 --- a/config/tab-translatable.php +++ b/config/tab-translatable.php @@ -20,8 +20,10 @@ * And choose the 'code_field' for example "en", "fr", "es"... */ 'database' => [ - 'model' => 'App\\Language', + 'model' => 'App\\Model\\Language', 'code_field' => 'lang', + 'sort_by' => 'id', + 'sort_direction' => 'asc' ], /* diff --git a/src/NovaTabTranslatable.php b/src/NovaTabTranslatable.php index 9003c6a..0a3c787 100644 --- a/src/NovaTabTranslatable.php +++ b/src/NovaTabTranslatable.php @@ -44,7 +44,12 @@ public function __construct(array $fields = []) parent::__construct($this->name); $config = config('tab-translatable'); if($config['source'] == 'database') - $this->locales = $config['database']['model']::pluck($config['database']['code_field'])->toArray(); + $this->locales = $config['database']['model']::query() + ->when(isset($config['database']['sort_by']), function($query) use($config) { + $query->orderBy($config['database']['sort_by'], $config['database']['sort_direction']); + }) + ->pluck($config['database']['code_field']) + ->toArray(); else $this->locales = $config['locales'];