Skip to content

Commit

Permalink
Add database sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
kongulov committed Nov 17, 2022
1 parent fb798c7 commit 16bed37
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
],

/*
Expand Down
4 changes: 3 additions & 1 deletion config/tab-translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
],

/*
Expand Down
7 changes: 6 additions & 1 deletion src/NovaTabTranslatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand Down

0 comments on commit 16bed37

Please sign in to comment.