Skip to content

Commit 7c56ae1

Browse files
committed
Update HasResourceTaxonomy.php
1 parent c7101f9 commit 7c56ae1

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/Traits/Taxonomy/HasResourceTaxonomy.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ protected static function createTaxonomyField(string $taxonomy, array $settings,
4141
$modelClass = $taxonomyService->getTaxonomyModel($taxonomy);
4242
$taxonomyService->validateTaxonomy($taxonomy);
4343
$isHierarchical = $settings['hierarchical'] ?? false;
44-
4544
$createFormClass = $settings['createForm'] ?? CoreTaxonomyCreateForm::class;
4645

4746
$commonConfig = [
@@ -55,28 +54,32 @@ protected static function createTaxonomyField(string $taxonomy, array $settings,
5554
]);
5655

5756
if ($validator->fails()) {
57+
5858
return $validator->errors()->first();
5959
}
6060

6161
$model = app($modelClass);
6262

6363
// Check if model is translatable
6464
if (method_exists($model, 'createWithTranslations')) {
65-
$locale = app()->getLocale();
65+
$locale = request()->get('lang') ?? app()->getLocale();
6666
$translations = [
6767
$locale => [
6868
'title' => $data['title'],
6969
'slug' => $data['slug'],
7070
],
7171
];
72+
\Illuminate\Support\Facades\Log::info('translations', $translations);
7273
$newTaxonomy = $model::createWithTranslations([], $translations);
7374
} else {
74-
// Handle non-translatable models
7575
$newTaxonomy = $model::create([
7676
'title' => $data['title'],
7777
'slug' => $data['slug'],
7878
]);
79+
\Illuminate\Support\Facades\Log::info('newTaxonomy', $newTaxonomy);
80+
7981
}
82+
\Illuminate\Support\Facades\Log::info('newTaxonomy', $newTaxonomy);
8083

8184
return $newTaxonomy->id;
8285
},
@@ -89,6 +92,7 @@ protected static function createTaxonomyField(string $taxonomy, array $settings,
8992
titleAttribute: 'title',
9093
parentAttribute: 'parent_id'
9194
)
95+
9296
->enableBranchNode()
9397
->searchable()
9498
->createOptionForm($commonConfig['createOptionForm'])
@@ -105,7 +109,7 @@ protected static function createTaxonomyField(string $taxonomy, array $settings,
105109
->when(method_exists($modelClass, 'whereHas'), function ($query) use ($search) {
106110
$query->whereHas('translations', function ($q) use ($search) {
107111
$q->where('title', 'like', sprintf('%%%s%%', $search))
108-
->where('locale', app()->getLocale());
112+
->where('locale', request()->get('lang') ?? app()->getLocale());
109113
});
110114
}, function ($query) use ($search) {
111115
$query->where('title', 'like', sprintf('%%%s%%', $search));
@@ -135,6 +139,7 @@ public static function getTaxonomyFilters(): array
135139
$taxonomyService = static::getTaxonomyService();
136140
$taxonomies = $taxonomyService->getTaxonomies();
137141
$resourceModel = static::getModel();
142+
138143
$resourceTable = app($resourceModel)->getTable();
139144

140145
return collect($taxonomies)->map(function ($settings, $taxonomy) use ($taxonomyService, $resourceTable): SelectFilter {
@@ -181,6 +186,7 @@ protected static function getTaxonomyColumns(): array
181186
$taxonomyService = static::getTaxonomyService();
182187
$taxonomies = $taxonomyService->getTaxonomies();
183188

189+
184190
return collect($taxonomies)->map(fn ($settings, $taxonomy): TagsColumn => TagsColumn::make($taxonomy)
185191
->label($settings['label'] ?? ucfirst((string) $taxonomy))
186192
->getStateUsing(function ($record) use ($taxonomy, $taxonomyService, $settings) {
@@ -200,7 +206,8 @@ protected static function getTaxonomyColumns(): array
200206
return $query->join('translations', function ($join) use ($modelTable, $modelClass) {
201207
$join->on('translations.translatable_id', '=', $modelTable.'.id')
202208
->where('translations.translatable_type', '=', $modelClass)
203-
->where('translations.locale', '=', app()->getLocale());
209+
->where('translations.locale', '=', request()->get('lang') ?? app()->getLocale());
210+
204211
})->pluck('translations.title');
205212
}, function ($query) use ($modelTable) {
206213
return $query->pluck($modelTable.'.title');

0 commit comments

Comments
 (0)