Skip to content

Displaying states in a dropdown #189

Answered by brendt
ju5t asked this question in Q&A
Sep 21, 2021 · 1 comments · 4 replies
Discussion options

You must be logged in to vote

There's no built-in support for generating that kind of data, since it's out of scope for this package.

The way we solved it is by adding a stateOptions(string $stateClass): array method on a base view model class in our projects. If you're not using view models you could write such a function somewhere else so that it's reusable.

It looks something like this:

    /**
     * @param string|\Spatie\ModelStates\State|array|\Illuminate\Support\Collection $stateOptions
     *
     */
    protected function stateOptions($stateOptions, Model $model): array
    {
        if (is_subclass_of($stateOptions, State::class)) {
            $stateOptions = $stateOptions::all();
        }

        if (is_…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@ju5t
Comment options

@brendt
Comment options

@kerrinhardy
Comment options

@brendt
Comment options

Answer selected by ju5t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants