#filter json to the table

233 messages · Page 1 of 1 (latest)

shell muskBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

south siren
#

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

granite yacht
#

i got this error Cannot access offset of type string on string

brave fable
granite yacht
#

okay thanks

brave fable
#

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

brave fable
granite yacht
#

its looks like this

#

in db

brave fable
#

Yes, if you don't cast it it will return as a string

granite yacht
#

model looks like this

brave fable
#

try value => 'array' instead of json

granite yacht
#

Cannot access offset of type string on string

#

i have this error again

granite night
#

Do you have empty values in the DB?

#

if some are null or '' you won't be able to decode it with the cast.

granite yacht
#

this is my table

#

any ideas ((

#

i trying to this about 5 days ...

brave fable
#

Can you dd the model?

so in

TextColumn::make('value')
    ->formatStateUsing(fn (Model $record): string => dd($record->value))
granite yacht
#

okay wait

brave fable
#

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

granite yacht
#

its returns 'gamarjoba'

#

like 'en' value

brave fable
#

Can you send a screenshot?

granite yacht
#

sure

brave fable
#

Mmmh

#

And about your code?

#

What's the exact code that ran?

granite yacht
#

code which u send me

#
    ->formatStateUsing(fn (Model $record): string => dd($record->value))```
#

this

#

i dd this

brave fable
#

And the model?

granite yacht
#

f

granite yacht
#

looks like this

brave fable
#

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

granite yacht
#

Idk for me its no problem just i want to do it

granite yacht
brave fable
#

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?

granite yacht
#

hm wait maybe its not my code only..

#

nah i iinstall it now

#

what i should do now ? 😄

brave fable
#

Follow the documentation of the package. It should give you a step by step proces how to configure it

granite yacht
#

ah okay thanks u ❤️

brave fable
#

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

granite yacht
#

just TextColumn::make('value')
?

brave fable
#

Yap I think so

granite yacht
#

i read it but there is no example how to add table column (((

#

like its shows me only english not both

brave fable
#

If you want both at the same time just do your original idea

granite yacht
#

nah i dont want this switcher,..

#

i already have something like this

brave fable
granite yacht
#

mnehh

#

okay 😄

#

okay but u should use now formatState?

brave fable
#

Check what data you're getting back from the model first

granite yacht
#

like when i use only this TextColumn::make('value')

#

its shows only en text

south siren
#

show us dd($model) instead of $model->value

granite yacht
#
                    ->formatStateUsing(fn (Model $record): string => dd($model))        
                        ])```
#

like this?

south siren
#

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.

granite yacht
#

error ```Undefined variable '$model'.intelephense(P1008)

#

but i dd $rceord and

south siren
#

it's record, my bad

granite yacht
#

if we go $record->attirubtes->value

#

there is

#

look

south siren
#

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"]

granite yacht
#

its null

#

json_decode makes it null

south siren
#

is the column on the database defined as text or json?

granite yacht
#

json

south siren
#

how are persisting the data on the database?

#

are json_encoding manually?

granite yacht
#

look

south siren
#

that doesnt answer my question xD

granite yacht
#

hm sorry then

#

what i shouold show u ? d

south siren
#

did you changed the creation hook function in the resource?

granite yacht
#
    {
        $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

south siren
#

it's weird that you casted the value to an array but still returns a string

granite yacht
#

🥲

south siren
#

try to run composer dumpautoload and php artisan optimize:clear

granite yacht
#

i did it next?

south siren
#

when u did dd($record) on the casts array does it show the value as an array?

granite yacht
#

wait

#

nahh look

#

its json

south siren
#

once again not what I asked xD thats the attributes, I asked for the casts (check a bit below that xD)

granite yacht
#

ah sorry 😄

#

yep its json

south siren
#

change it to array

granite yacht
#

how 😢

#

in model?

#

okay i change it

south siren
#

the same way you casted as json, put array xD

granite yacht
#

its array now

south siren
#

then dd(json_decode($record->value))

granite yacht
#

its null again

brave fable
south siren
#

what if you do dd($record->value) is it a string or an array?

granite yacht
#

ah no

#

its not null

#

now its shows me 'gamarjoba'

brave fable
#

Can screenshot your code and the dd response?

granite yacht
#

like only en value

south siren
#

are you using any localization package?

granite yacht
brave fable
brave fable
#

"HasTranslation" & "$translatable = ['value']"

south siren
#

maybe the package has a function to get a translatablefield by locale

granite yacht
#

idk its not my code btw 💀

brave fable
south siren
#

whats the package name? check composer json if you can

granite yacht
#
        "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"```
brave fable
#

"filament/spatie-laravel-translatable-plugin": "^3.2",
I thought you removed this package?

granite yacht
#

yes i remove it

brave fable
#

Why is it still there then?

granite yacht
#

tf its not removed

#

why

#

wait 😄

brave fable
#

composer remove filament/spatie-laravel-translatable-plugin

granite yacht
#

yep i run it

brave fable
#

and there's also : spatie/laravel-translatable"

granite yacht
#

its removed now thanks

south siren
#

wait, dont remove the last one and instead of the dd put $record->getTranslation('value', 'en'); or $record->getTranslation('value, 'sl');

granite yacht
#

fuck

#

$record->getTranslation('value', 'en')

#

this works now

brave fable
#

Ah nice

granite yacht
#

thanks lol how when

#

tf

south siren
brave fable
#

I didn't know you were already using the spatie package

#

Well that wouldve made it easier haha

south siren
#

and on the second textcolumn you call the sl locale

granite yacht
#

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

south siren
#

change to TextColumn::make('value_sl')

granite yacht
#

now slovenian isnt showed

#

😄

south siren
#

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()

granite yacht
#

hmm okay i will try 😄

south siren
#

or maybe a cheeky breeky way (don't think its possible) but make('value.en') and make('value.sl') 😆

granite yacht
#

yep i try it

#

also i tryt value->en but

#

not works

south siren
#

yeah attributes is the way to go then

granite yacht
#

hmm how can u tellme simple way forthat?

south siren
#
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')
granite yacht
#

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

south siren
#

$value->sl? xD

granite yacht
#

when i justimport attribiute using like this ```use Attribute;

south siren
#

or dd($value)

granite yacht
#

hmm wait i wiill try something

south siren
#

use Illuminate\Database\Eloquent\Casts\Attribute;

granite yacht
#

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

`

south siren
#

try $this->getTranslation('value', 'sl')

#

or $this->getTranslation($this->value, 'sl')

granite yacht
#

nahh same error

south siren
#

get: fn (string $value) => dd($this)

granite yacht
#

i try it too

#

but same error lol

south siren
#

but the error stack is pointing to the model file or the resource?

granite yacht
#

model

brave fable
south siren
#

yeah but I think there's more to it because I've used this package before and never had this problem

granite yacht
#

hmm btw me

#

i have no idea why 😄

south siren
#

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),
        );

granite yacht
#

i remove cast and write this but same error

south siren
#

what about

TextColumn::make('value_sl')
    ->state(function (Model $record): string {
        return $record->getTranslation('value','sl);
    })
#

remove the attribute stuff from the model

granite yacht
#

omg

#

ur genius bro

#

but wait when i click edit

#

value is empty 😢 any idea how to fill automaticly this ?

south siren
#

the edit is another issue

#

I dont know the code for the edit

granite yacht
#

okay i will try by myself first

#

thanks btw for ur time and help

#

thank u soo much ❤️

south siren
#

no prob

granite yacht
#

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

granite yacht
# granite yacht

and in another translations like there when i click name is filled but in mine not