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

Recipe: use with spatie/laravel-tags #123

Open
cord opened this issue Oct 5, 2021 · 0 comments
Open

Recipe: use with spatie/laravel-tags #123

cord opened this issue Oct 5, 2021 · 0 comments
Labels
tutorial Nothing's wrong, just a helpful tip on how to do something.

Comments

@cord
Copy link

cord commented Oct 5, 2021

UseCase: only pre-defined tags should be selectable no dynamic creation of tags should be possible as with https://novapackages.com/packages/spatie/nova-tags-field

// define type to select from

            $tagType = 'myType';

//(...)

            Multiselect::make(__("Tags"), 'tags')
                ->options(function () use ($tagType) {
                    $query = resolve(config('tags.tag_model', Tag::class))->query();

                    $filter_containing = null;
                    $filter_type = $tagType;
                    $limit = null;

                    if ($filter_containing) {
                        $query->containing($filter_containing);
                    }

                    if ($filter_type) {
                        $query->where('type', $filter_type);
                    }

                    if ($limit) {
                        $query->limit($limit);
                    }

                    $sorted = $query->get()->sortBy(function (Tag $tag) {
                        return strtolower($tag->name);
                    })->values();

                    $tags = $sorted->mapWithKeys(function (Tag $tag, $key) {
                        return [$tag->name => $tag->name];
                    });
                    return $tags;
                })
                ->resolveUsing(function ($tags) {
                    return $tags->pluck('name');
                })
                ->fillUsing(function ($request, $model, $attribute, $requestAttribute) use ($tagType) {
                    $requestValue = $request[$requestAttribute];

                    $tagNames = array_values($requestValue ?? []);
                    $tagNames = array_filter($tagNames); // remove empty values

                    $class = get_class($model);
                    $class::saved(function ($model) use ($tagNames) {
                        $model->syncTagsWithType($tagNames, $tagType ?? null);
                    });
                })
                ->placeholder(__('Add Tags')) // Placeholder text
@Tarpsvo Tarpsvo added the tutorial Nothing's wrong, just a helpful tip on how to do something. label Jul 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tutorial Nothing's wrong, just a helpful tip on how to do something.
Projects
None yet
Development

No branches or pull requests

2 participants