Skip to content

Commit 404d4f5

Browse files
Update HasResourceTaxonomy.php
1 parent 0df673a commit 404d4f5

File tree

1 file changed

+68
-23
lines changed

1 file changed

+68
-23
lines changed

packages/core/src/Traits/Taxonomy/HasResourceTaxonomy.php

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,25 @@ protected static function createTaxonomyField(string $taxonomy, array $settings,
5858
return $validator->errors()->first();
5959
}
6060

61-
$locale = app()->getLocale();
62-
$translations = [
63-
$locale => [
61+
$model = app($modelClass);
62+
63+
// Check if model is translatable
64+
if (method_exists($model, 'createWithTranslations')) {
65+
$locale = app()->getLocale();
66+
$translations = [
67+
$locale => [
68+
'title' => $data['title'],
69+
'slug' => $data['slug'],
70+
]
71+
];
72+
$newTaxonomy = $model::createWithTranslations([], $translations);
73+
} else {
74+
// Handle non-translatable models
75+
$newTaxonomy = $model::create([
6476
'title' => $data['title'],
6577
'slug' => $data['slug'],
66-
],
67-
];
68-
69-
$newTaxonomy = app($modelClass)::createWithTranslations([], $translations);
78+
]);
79+
}
7080

7181
return $newTaxonomy->id;
7282
},
@@ -88,29 +98,39 @@ protected static function createTaxonomyField(string $taxonomy, array $settings,
8898

8999
return Select::make($taxonomy)
90100
->multiple()
91-
->options(fn () => app($modelClass)::with('translations')
101+
->options(fn () => app($modelClass)::query()
102+
->when(method_exists($modelClass, 'with'), fn ($query) => $query->with('translations'))
92103
->get()
93104
->mapWithKeys(function ($item) {
94-
$locale = request()->query('lang', app()->getLocale());
95-
$translation = $item->translate($locale) ?? $item->translate(app()->getLocale());
96-
97-
return [$item->id => $translation ? $translation->title : 'ID: '.$item->id];
105+
if (method_exists($item, 'translate')) {
106+
$locale = request()->query('lang', app()->getLocale());
107+
$translation = $item->translate($locale) ?? $item->translate(app()->getLocale());
108+
return [$item->id => $translation ? $translation->title : 'ID: ' . $item->id];
109+
}
110+
return [$item->id => $item->title];
98111
})
99112
->toArray()
100113
)
101114
->getSearchResultsUsing(
102-
fn (string $search) => app($modelClass)::with('translations')
103-
->whereHas('translations', function ($query) use ($search) {
104-
$query->where('title', 'like', sprintf('%%%s%%', $search))
105-
->where('locale', app()->getLocale());
115+
fn (string $search) => app($modelClass)::query()
116+
->when(method_exists($modelClass, 'with'), fn ($query) => $query->with('translations'))
117+
->when(method_exists($modelClass, 'whereHas'), function ($query) use ($search) {
118+
$query->whereHas('translations', function ($q) use ($search) {
119+
$q->where('title', 'like', sprintf('%%%s%%', $search))
120+
->where('locale', app()->getLocale());
121+
});
122+
}, function ($query) use ($search) {
123+
$query->where('title', 'like', sprintf('%%%s%%', $search));
106124
})
107125
->limit(50)
108126
->get()
109127
->mapWithKeys(function ($item) {
110-
$locale = app()->getLocale();
111-
$translation = $item->translate($locale);
112-
113-
return [$item->id => $translation ? $translation->title : 'ID: '.$item->id];
128+
if (method_exists($item, 'translate')) {
129+
$locale = app()->getLocale();
130+
$translation = $item->translate($locale);
131+
return [$item->id => $translation ? $translation->title : 'ID: ' . $item->id];
132+
}
133+
return [$item->id => $item->title];
114134
})
115135
->toArray()
116136
)
@@ -137,7 +157,19 @@ public static function getTaxonomyFilters(): array
137157
return SelectFilter::make($taxonomy)
138158
->label($settings['label'] ?? ucfirst($taxonomy))
139159
->multiple()
140-
->options(fn () => $taxonomyModel::pluck('title', 'id')->toArray())
160+
->options(fn () => app($taxonomyModel)::query()
161+
->when(method_exists($taxonomyModel, 'with'), fn ($query) => $query->with('translations'))
162+
->get()
163+
->mapWithKeys(function ($item) {
164+
if (method_exists($item, 'translate')) {
165+
$locale = app()->getLocale();
166+
$translation = $item->translate($locale);
167+
return [$item->id => $translation ? $translation->title : 'ID: ' . $item->id];
168+
}
169+
return [$item->id => $item->title];
170+
})
171+
->toArray()
172+
)
141173
->query(function (Builder $query, array $data) use ($pivotTable, $foreignKey, $relatedKey, $resourceTable): void {
142174
$selectedIds = $data['values'] ?? [];
143175
if (! empty($selectedIds)) {
@@ -172,7 +204,15 @@ protected static function getTaxonomyColumns(): array
172204
return DB::table($table)
173205
->join($modelTable, sprintf('%s.%s', $table, $relatedKey), '=', $modelTable.'.id')
174206
->where(sprintf('%s.%s', $table, $foreignKey), $record->id)
175-
->pluck($modelTable.'.title')
207+
->when(method_exists($model, 'with'), function ($query) use ($modelTable, $modelClass) {
208+
return $query->join('translations', function ($join) use ($modelTable, $modelClass) {
209+
$join->on('translations.translatable_id', '=', $modelTable.'.id')
210+
->where('translations.translatable_type', '=', $modelClass)
211+
->where('translations.locale', '=', app()->getLocale());
212+
})->pluck('translations.title');
213+
}, function ($query) use ($modelTable) {
214+
return $query->pluck($modelTable.'.title');
215+
})
176216
->toArray();
177217
})
178218
->toggleable(isToggledHiddenByDefault: true)
@@ -185,7 +225,12 @@ protected static function handleTaxonomies(Model $record, array $data): void
185225
$taxonomyService = static::getTaxonomyService();
186226
foreach (array_keys($taxonomyService->getTaxonomies()) as $taxonomy) {
187227
if (isset($data[$taxonomy])) {
188-
$record->$taxonomy()->sync($data[$taxonomy]);
228+
$relationshipName = $taxonomyService->getTaxonomyRelationship($taxonomy);
229+
230+
// Use the relationship name from the taxonomy service
231+
if (method_exists($record, $relationshipName)) {
232+
$record->$relationshipName()->sync($data[$taxonomy]);
233+
}
189234
}
190235
}
191236
}

0 commit comments

Comments
 (0)