Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search with Spatie Translatable field #40

Open
sezohessen opened this issue Feb 15, 2023 · 1 comment
Open

Search with Spatie Translatable field #40

sezohessen opened this issue Feb 15, 2023 · 1 comment

Comments

@sezohessen
Copy link

I need to search with translatable field

public static $searchRelations = [
        'category' => ['title'],
    ];

title has translatable property of Spatie package

public $translatable = ['title'];
@anditsung
Copy link

I need to search with translatable field

public static $searchRelations = [
        'category' => ['title'],
    ];

title has translatable property of Spatie package

public $translatable = ['title'];

create a new class that implements search
im not using spatie package and dont know how it works.

this is an example how i search on laravel nova actions using moprh on the target

class MorphSearch implements Search
{
    /**
     * Searchable columns.
     *
     * @var array
     */
    protected $morphRelation;

    /**
     * Instantiate a new search query instance.
     *
     * @param array $columns
     */
    public function __construct(array $morphRelation)
    {
        $this->morphRelation = $morphRelation;
    }

    public function apply(Builder $query, string $relation, string $search): Builder
    {
        foreach ($this->morphRelation as $morphClass => $columns) {
            if (class_exists($morphClass) && is_subclass_of($morphClass, Model::class)) {
                $query->orWhereHasMorph($relation, [$morphClass], function ($query) use ($columns, $relation, $search) {
                    return (new ColumnSearch($columns))->apply($query, $relation, $search);
                });
            }
        }

        return $query;
    }
}

nova resource

...

public static function searchableRelations(): array
    {
        return [
            'target' => new MorphSearch([
                'App\Models\Role' => ['name'],
                'App\Models\User' => ['name', 'username', 'email'],
                'App\Models\Permission' => ['name'],
                'App\Models\Invite' => ['email'],
            ])
        ];
    }

...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants