F
Filament15mo ago
Joe

JSON columns issue

I'm creating a new application which has multiple languages, Arabic and english, so i made the title column type as JSON to accept multiple types eg. (title:{"ar":"lorem","en":"impsum"}) then i managed to install spatie translateable package and plugin. However in Select Input or SelectFilter the data appears as showing in the pictures, the Select input in the create/edit page can be sroted by adding options() with a query that gets the required data however that doesn't seem to work in SelectFilter for some reason.
No description
No description
Solution:
Thanks to @awcodes the issue got sorted after raising the case as an issue in filament repo. here is the link to solution: https://github.com/filamentphp/filament/issues/9033#event-10646961042...
GitHub
Translatable Columns Appears as JSON on the Dashboard · Issue #903...
Package filament/filament Package Version v3.0.63 Laravel Version v10.25.2 Livewire Version v3.0.5 PHP Version 8.1.0 Problem description Whenever there is a column that is translatable, it appears ...
Jump to solution
25 Replies
awcodes
awcodes15mo ago
I've seen similar with relationships, I'm not sure if it's a bug in Spatie's package or not, but it looks like it's not parsing the locale when querying a relationship.
Joe
JoeOP15mo ago
hey there @awcodes, Thank you for responding, I'm glad that I managed to reach you, the issue is that it can be fixed if you managed to add ->options() with a query that gets the related data but it only works with Select, however in SelectFilter it doesn't seem to work the image provided is in the Select case you can see that adding options() fixed it
No description
No description
Joe
JoeOP15mo ago
but in SelectFilter that doesn't seem to work
No description
No description
awcodes
awcodes15mo ago
yea, i'm seeing it happen in general. not even with a select. that's why i'm wondering if it's an issue in the Spatie package. like, in tinkerwell, if i just grab a model from the DB and look at it's relationships, the related models have both languages instead of the current locale, while the parent record is translated correctly
Joe
JoeOP15mo ago
I think that's correct, due to the fact that the CategoryResource shows the data as it should be and even has the availability to switch between languages when the translatable plugin is used. Is there any sort of workaround that i can do to make it work? I tried all the available options and it doesn't seem to work 😓
awcodes
awcodes15mo ago
I think there's actually an open issue on the repo for this. I'm not sure of a work around though, other than possibly modifying the query to explicitly return the current locale's value.
Joe
JoeOP15mo ago
by modifying query you mean modify it in ->options() or ->relationship() ? because i tried using options() and didn't work while relationships could figure out how as it kept giving out errors As an update, this what've reached to:
->relationship('categories', 'title', modifyQueryUsing: function (Builder $query) {
$query->each(fn ($category) => $category->title = json_decode($category->title, true));
})
->relationship('categories', 'title', modifyQueryUsing: function (Builder $query) {
$query->each(fn ($category) => $category->title = json_decode($category->title, true));
})
but yet still giving the same outcome for some reason despite the fact that when i run dd() it shows the correct outcome
awcodes
awcodes15mo ago
Odd What if you tried:
->relationship('categories', 'title', modifyQueryUsing: function (Builder $query) {
$locale = app()->currentLocale();
$query->each(fn ($category) use ($locale) => $category->title->{$locale});
})
->relationship('categories', 'title', modifyQueryUsing: function (Builder $query) {
$locale = app()->currentLocale();
$query->each(fn ($category) use ($locale) => $category->title->{$locale});
})
Joe
JoeOP15mo ago
sorry for responding late, I just tried it and that was the outcome
No description
Joe
JoeOP15mo ago
I adjusted your code to be the below and the error was gone, but still giving the same outcome:
->relationship('categories', 'title', modifyQueryUsing: function (Builder $query) {
$locale = app()->currentLocale();
$query->each(fn ($category) => $category->title
->{$locale});
})
->relationship('categories', 'title', modifyQueryUsing: function (Builder $query) {
$locale = app()->currentLocale();
$query->each(fn ($category) => $category->title
->{$locale});
})
awcodes
awcodes15mo ago
Maybe?
->relationship('categories', 'title', modifyQueryUsing: function (Builder $query) {
$locale = app()->currentLocale();
$query->each(fn ($category) => json_decode($category->title)[$locale]);
})
->relationship('categories', 'title', modifyQueryUsing: function (Builder $query) {
$locale = app()->currentLocale();
$query->each(fn ($category) => json_decode($category->title)[$locale]);
})
definitely something odd going on here.
Joe
JoeOP15mo ago
this error came up
No description
Joe
JoeOP15mo ago
but there is an interesting thing happening here
Joe
JoeOP15mo ago
I tried running dump on the code like this :
->relationship('categories', 'title', modifyQueryUsing: function (Builder $query) {
$locale = app()->currentLocale();
$query->each(fn ($category) => dump($category->title));
})
->relationship('categories', 'title', modifyQueryUsing: function (Builder $query) {
$locale = app()->currentLocale();
$query->each(fn ($category) => dump($category->title));
})
and that was the outcome in the photo. i removed the json_decode as it was the source of the error then the [locale] gave an error as it was considered as an empty key. so i thought of applying dump() on $category->title to see what it gives out and surprisingly it gives the desired outcome!! so why it showing json despite being printed as string ?
No description
Joe
JoeOP15mo ago
and btw, these values are the "en" key values of each row so it's a perfect response, but when i remove the dump it prints the data as json again!
Joe
JoeOP15mo ago
As additional info these accordingly are Category.php Service.php models the ServiceResource is what I'm trying to implement the category filter on
Category
Service
Joe
JoeOP15mo ago
I tried changing json to array and it was the same outcome
awcodes
awcodes15mo ago
Hmm. Definitely a head scratcher. I’ll let you know if I think of something.
Joe
JoeOP15mo ago
I really want to thank you for the time that you've given and being patient with me 🙏
awcodes
awcodes15mo ago
Thank you for being patient with me. Lol.
Joe
JoeOP14mo ago
@awcodes Hey there, We discussed this issue about a month ago unfortunately i tried all means possible to tackle it but it still there, I even upgraded to Filament v3.0.63 but with no luck. i even tried to install behzansalleh/languageswitcher in order to switch between languages in the hopes of fixing it but it did nothing. In the picture attached you're going to find that the select is reversed and that's due to the whole admin panel is switched to another language (Arabic) but still the same
No description
awcodes
awcodes14mo ago
Yea, I'm seeing this in other places too. I'm still not sure if there's a bug in Spatie's package or Filament where translations are getting read out with relationships.
Joe
JoeOP14mo ago
If this helps, it mostly occurs with options that are generated by a relationship, like for example, the picture is for a table of services and being filtered by categories, they are connected via many-to-many relationship and relationship("categories","title") is added I tried removing the relationship and adding the options via - options() only but it returned an error saying categories doesn't exist, I changed it to category_id , but still no luck If that helps too, I've been working on a project with the same requirements for language switcher and it's working but that's filament 2 not 3
Joe
JoeOP14mo ago
Hey there, sorry to be a bother, Is there any updates about this issue? because i tried it on a checkbox list and gave the same outcome and there seems to be no workaround for it
No description
No description
Solution
Joe
Joe13mo ago
Thanks to @awcodes the issue got sorted after raising the case as an issue in filament repo. here is the link to solution: https://github.com/filamentphp/filament/issues/9033#event-10646961042
GitHub
Translatable Columns Appears as JSON on the Dashboard · Issue #903...
Package filament/filament Package Version v3.0.63 Laravel Version v10.25.2 Livewire Version v3.0.5 PHP Version 8.1.0 Problem description Whenever there is a column that is translatable, it appears ...
Want results from more Discord servers?
Add your server