-
-
Notifications
You must be signed in to change notification settings - Fork 171
Closed as not planned
Labels
Description
I wrote a simple solution how to integrate in to Laravel Filemant 2.x
I edit this issue integration, becouse found a better way.
- Create trait Translatable
namespace App\Filament\Traits;
use Illuminate\Database\Eloquent\Builder;
trait Translatable
{
// This override get translations fields
protected function fillForm(): void
{
$this->callHook('beforeFill');
$data = $this->getRecord()->attributesToArray();
foreach (static::getRecord()->getTranslationsArray() as $key => $value) {
$data[$key] = $value;
}
$data = $this->mutateFormDataBeforeFill($data);
$this->form->fill($data);
$this->callHook('afterFill');
}
// This override get SQL optimization and get all translations
protected function getTableQuery(): Builder
{
return static::getResource()::getEloquentQuery()->with('translations');
}
}
- For your ModelResource -> Pages -> EditRecords and ListRecords add your new trait Translatable
- In your ModelResource method form(Form $form): Form add translation fields
Forms\Components\TextInput::make('lv.name'),
Forms\Components\TextInput::make('lt.name'),
- In your ModelResource method table(Table $table): Table add translation column, i show you example, how use searchable() with translations, this search with all locales
Tables\Columns\TextColumn::make('name:lv')
->searchable(query: function (Builder $query, string $search): Builder {
return $query->whereTranslationLike('name', "%{$search}%");
})
->label('Name (LV)'),
Tables\Columns\TextColumn::make('name:lt')
->toggleable()
->label('Name (LT)'),
- In config translatable, you only add array locales, example: ['lv', 'lt']
Now, dont work custom Global search, but... if added this example to ModelResource, Global Search work by default locale and bad SQL optimization...
protected static ?string $recordTitleAttribute = 'translation.name';
public static function getGlobalSearchResultTitle(Model $record): string
{
// $record->{'name:de'};
return $record->name;
}
Update: How use relationship
- First we create ModelRelationManager, how about say documentation Filemanet
- In methods $form and $table, we create fields and columns, i all copy from ModelResource
- If you call ModelRelationManager -> actions[] -> method edit(), all translations fields its clear... its problem
Solution
- Add trait Translatable to ModelRelationManager
- We change EditAction in ->actions(), use command artisan route:list for get your route. This example open your resource in new tab, if delete openUrlInNewTab() - opened in this page. Example:
Tables\Actions\EditAction::make()
->url(fn (City $record): string => route('filament.resources.cities.edit', $record))
->openUrlInNewTab(),
- If you want open modal with translations fields, this example filling default data
Tables\Actions\EditAction::make('modal_edit_with_data')
->mountUsing(function (Forms\ComponentContainer $form, City $record) {
$data = [];
// Get all data in array
$dataRecord = $record->toArray();
// Save in $data by key = value
foreach ($dataRecord as $key => $value) {
$data[$key] = $value;
}
// Get all translations in array and save in $data by key = value
foreach ($record->getTranslationsArray() as $key => $value) {
$data[$key] = $value;
}
// Filled your data to $form and return
return $form->fill($data);
})
->form([......all fields, i copy from ModelResource -> $form().....])
- Add override method
// This override get SQL optimization and get all translations for ModelRelationManager
public function getRelationship(): Relation | Builder
{
return $this->getOwnerRecord()->{static::getRelationshipName()}()->with(['translations']);
}
It's work
All methods create, edit, delete and relationships work.
P.S Sorry for my english... ;)
makhweeb, inalto, vkarchevskyi and NikitaPilezmakhweeb, inalto, Oleksandr-Moik, vkarchevskyi and NikitaPilez