Skip to content

Add support Filament and simple solution #306

@Solunsky

Description

@Solunsky

I wrote a simple solution how to integrate in to Laravel Filemant 2.x
I edit this issue integration, becouse found a better way.

  1. 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');
    }
}
  1. For your ModelResource -> Pages -> EditRecords and ListRecords add your new trait Translatable
  2. In your ModelResource method form(Form $form): Form add translation fields
Forms\Components\TextInput::make('lv.name'),
Forms\Components\TextInput::make('lt.name'),
  1. 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)'),
  1. 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

  1. First we create ModelRelationManager, how about say documentation Filemanet
  2. In methods $form and $table, we create fields and columns, i all copy from ModelResource
  3. If you call ModelRelationManager -> actions[] -> method edit(), all translations fields its clear... its problem

Solution

  1. Add trait Translatable to ModelRelationManager
  2. 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(),
  1. 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().....])
  1. 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... ;)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions