#filter json to the table
233 messages · Page 1 of 1 (latest)
Are you using any package for handling translations?
if you have that column casted as array in the Model class you can try this
TextColumn::make('value')
->formatStateUsing(fn (Model $record): string => $record->value["en"])
TextColumn::make('value')
->formatStateUsing(fn (Model $record): string => $record->value["sl"])
something like that, not tested
i got this error Cannot access offset of type string on string
You might wanna look at this package, seems to do exactly what you want:
https://filamentphp.com/plugins/filament-spatie-translatable
okay thanks
But if you want to keep with your original idea. You might want to check if you casted the json properly in your "Translation" model
Because this error makes it seem that it gets a string as value instead of an array (or json). Model casting would solve that
Yes, if you don't cast it it will return as a string
try value => 'array' instead of json
Do you have empty values in the DB?
if some are null or '' you won't be able to decode it with the cast.
Can you dd the model?
so in
TextColumn::make('value')
->formatStateUsing(fn (Model $record): string => dd($record->value))
okay wait
Also not sure if its allowed to have two columns with the same name 🤔
But don't quote me on that, Toeknee might know that better
Can you send a screenshot?
code which u send me
->formatStateUsing(fn (Model $record): string => dd($record->value))```
this
i dd this
And the model?
f
Might be the translation package intervering. Are you planning on using it? Or do you want to keep your first solution?
If you're using the package I'm pretty sure you don't need to do the ->formatStateUsing() method
Idk for me its no problem just i want to do it
okay i should install this right?
I think it would work for you, but if I look at your model you already have the "HasTranslations" and public $translatable properties. So I assume you already have it installed?
hm wait maybe its not my code only..
nah i iinstall it now
what i should do now ? 😄
Follow the documentation of the package. It should give you a step by step proces how to configure it
ah okay thanks u ❤️
The package uses the spatie-translatable package underwater, so don't forget to read that documentation as well
When you're done with that remove the ->formatState() method from your column
just TextColumn::make('value')
?
Yap I think so
i read it but there is no example how to add table column (((
like its shows me only english not both
If you want both at the same time the package won't work for you. In the documentation it says you need to add a switcher in the table header
If you want both at the same time just do your original idea
So delete the package then, remove all the configuration, debug the values you're getting back from the model and see from there
Check what data you're getting back from the model first
show us dd($model) instead of $model->value
yes
having packages won't fix the problem because normally the package will return the value based on the locale. He wants to output all the languages on the table.
it's record, my bad
ok, what about dd(json_decode($record->value)) ?
if you convert it to an array then you can json_decode($record->value)["en"] or ["sl"]
is the column on the database defined as text or json?
json
that doesnt answer my question xD
did you changed the creation hook function in the resource?
{
$tabs = self::getTranslationTabs($form->getRecord());
return $form
->schema([
Grid::make()->schema([
Tabs::make('Languages')->tabs($tabs),
Section::make()->schema([
TextInput::make('alias')
->label('Alias')
->required()
->unique(ignoreRecord: true),
])->columns(1),
])->columns(1),
]);
}
this is whole code btw
it's weird that you casted the value to an array but still returns a string
🥲
try to run composer dumpautoload and php artisan optimize:clear
i did it next?
when u did dd($record) on the casts array does it show the value as an array?
once again not what I asked xD thats the attributes, I asked for the casts (check a bit below that xD)
change it to array
the same way you casted as json, put array xD
its array now
then dd(json_decode($record->value))
its null again
Don't think you need to json_decode if the field is casted to array no?
probably xD
what if you do dd($record->value) is it a string or an array?
i try it and its null
ah no
its not null
now its shows me 'gamarjoba'
Can screenshot your code and the dd response?
are you using any localization package?
I think he is
yeah missed that
maybe the package has a function to get a translatablefield by locale
idk its not my code btw 💀
How is it not your code?
whats the package name? check composer json if you can
someone else write it..
"barryvdh/laravel-dompdf": "^3.0",
"blade-ui-kit/blade-heroicons": "^2.3",
"cviebrock/eloquent-sluggable": "^10.0",
"filament/actions": "^3.2",
"filament/filament": "3.2",
"filament/spatie-laravel-media-library-plugin": "^3.2",
"filament/spatie-laravel-translatable-plugin": "^3.2",
"flowframe/laravel-trend": "^0.2.0",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^10.10",
"laravel/sanctum": "^3.3",
"laravel/tinker": "^2.8",
"livewire/livewire": "^3.4",
"mcamara/laravel-localization": "^1.8",
"phpseclib/phpseclib": "~3.0",
"propaganistas/laravel-phone": "^5.3",
"pxlrbt/filament-activity-log": "^1.0",
"saloonphp/xml-wrangler": "^1.2",
"solution-forest/filament-tree": "^2.1",
"spatie/laravel-permission": "^6.4",
"spatie/laravel-sitemap": "^7.2",
"spatie/laravel-translatable": "^6.6",
"staudenmeir/belongs-to-through": "^2.5",
"tymon/jwt-auth": "^2.1",
"webbingbrasil/filament-copyactions": "^3.0"```
"filament/spatie-laravel-translatable-plugin": "^3.2",
I thought you removed this package?
yes i remove it
Why is it still there then?
composer remove filament/spatie-laravel-translatable-plugin
yep i run it
and there's also : spatie/laravel-translatable"
its removed now thanks
wait, dont remove the last one and instead of the dd put $record->getTranslation('value', 'en'); or $record->getTranslation('value, 'sl');
Ah nice
I didn't know you were already using the spatie package
Well that wouldve made it easier haha
and on the second textcolumn you call the sl locale
hm but wait
->label("English Value")
->formatStateUsing(fn (Model $record): string =>$record->getTranslation('value', 'en')),
TextColumn::make('value')
->label("Slovenian Value")
->formatStateUsing(fn (Model $record): string =>$record->getTranslation('value', 'sl')) ])
i write this
and its shows me only slovenian
change to TextColumn::make('value_sl')
because the make parameter is linked to a column name I bet
make an attribute on the model for the sl translation and put it on the make()
hmm okay i will try 😄
or maybe a cheeky breeky way (don't think its possible) but make('value.en') and make('value.sl') 😆
yeah attributes is the way to go then
hmm how can u tellme simple way forthat?
On Model
/**
* Get the value's sl .
*/
protected function valueSl(): Attribute
{
return Attribute::make(
get: fn (string $value) => $this->getTranslation($value, 'sl'),
);
}
Then on the resource
TextColumn::make('value_sl')
App\Components\Data\Translations\Models\Translations::App\Components\Data\Translations\Models{closure}(): Argument #1 ($value) must be of type string, null given
error is in there
in modle
but wait
$value->sl? xD
or dd($value)
hmm wait i wiill try something
use Illuminate\Database\Eloquent\Casts\Attribute;
yes and i have error `App\Components\Data\Translations\Models\Translations::App\Components\Data\Translations\Models{closure}(): Argument #1 ($value) must be of type string, null given
`
try $this->getTranslation('value', 'sl')
or $this->getTranslation($this->value, 'sl')
nahh same error
get: fn (string $value) => dd($this)
but the error stack is pointing to the model file or the resource?
Should work normally, but the translation package is probably intervering
yeah but I think there's more to it because I've used this package before and never had this problem
remove the cast to array because if you're using the package it should handle itself
return Attribute::make(
get: fn (string $value) => dd($value),
);
i remove cast and write this but same error
what about
TextColumn::make('value_sl')
->state(function (Model $record): string {
return $record->getTranslation('value','sl);
})
remove the attribute stuff from the model
omg
ur genius bro
but wait when i click edit
value is empty 😢 any idea how to fill automaticly this ?
okay i will try by myself first
thanks btw for ur time and help
thank u soo much ❤️
no prob
like i have same issue in another component and its works for another component but for this not
` public static function form(Form $form): Form
{
$tabs = self::getTranslationTabs($form->getRecord());
return $form
->schema([
Grid::make()->schema([
Tabs::make('Languages')->tabs($tabs),
])->columns(1),
Section::make()->schema([
TextInput::make('alias')
->label('Alias')
->required()
->unique(ignoreRecord: true),
])->columns(1),
]);
}
`
like this is my code
public static function form(Form $form): Form { $tabs = self::getTranslationTabs($form->getRecord()); return $form ->schema([ Grid::make()->schema([ Tabs::make('Languages')->tabs($tabs) ])->columns(1), Select::make('country_id')->label('Country')->placeholder('აირჩიეთ') ->options(Country::all()->pluck('name', 'id')) ->relationship( 'country', 'name', modifyQueryUsing: function (Builder $query, string $operation) { $query->selectRaw("id, json_unquote(json_extract(name,'$.en')) as name"); }, ) ->preload(), ]); } and this is another component code
and in another translations like there when i click name is filled but in mine not