How to make a selection dropDown based on table with translatable name field?

109 Views Asked by At

In laravel 10 app with filament/filament 3.0-stable and spatie/laravel-translatable 6.5.3 I need to make a selection dropDown based on table with translatable name field

Schema::create('quiz_categories', function (Blueprint $table) {
    $table->id();
    $table->json('name')->comment('This column is used for i18n support');
    $table->boolean('active')->default(false);
    $table->timestamps();
});

and in model app/Models/QuizCategory.php :

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Validation\Rule;
use Spatie\Translatable\HasTranslations;

class QuizCategory extends Model
{
    use HasTranslations;

    protected $table = 'quiz_categories';
    public $translatable = ['name'];
    protected $primaryKey = 'id';
    public $timestamps = false;
    protected $casts = [
        'active' => 'boolean',
        'name' => 'json'
    ];
    protected $fillable = ['name', 'active'];

In filament resource I try to define a selection :

Select::make('quiz_category_id')
      ->relationship('quizCategory', 'name',  modifyQueryUsing: fn (Builder $query) => $query->orderByRaw('name->"$.en"', 'desc'))
    ->getOptionLabelFromRecordUsing(fn (QuizCategory $record) => "{$record->{'name->"$.en"'}}")
    ->searchable(['name->"$.en"'])

But I got run time error :

SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
SELECT
  *
FROM
  "quiz_categories"
WHERE
  "quiz_categories"."id" = 1
ORDER BY
  name -> "$.en"
limit
  1

Looks like condition for order by is invalid. Field name in quiz_categories table always hasdefault "en".

How to fix it ?

Now app runs under Postgres 14, I need decision both for Postgres and MySql...

0

There are 0 best solutions below