skip to Main Content

I am using Laratrust with laravel to display records from the database according to roles. Every record has a role type field (example to table below).

enter image description here

In my blade view, I want to display them as the code (the variable is called $types).

<select name="type" id="" class="form-control">
    @foreach ($types as $type)
        @role($type->role)
        <option value="{{ $type->id }}">
            {{ $type->name }} {{ $type->role }}
        </option>
        @endrole
    @endforeach
</select>

It shows nothing. My data is correct. I tried to write the roles type by hand, and it worked. If I left the @role() empty, it shows an error, so it is not a problem of the role interpreted before the variable is passed. Any help would be appreciated.

2

Answers


  1. Chosen as BEST ANSWER

    the variable type was String and @role only worked with arrays so I add an accessor in the model to decode the data before getting it.


  2. You can try this

    <select name="type" id="" class="form-control">
        @foreach ($types as $type)
            @if(auth()->check() && auth()->user()->hasRole($type->role))
                <option value="{{ $type->id }}">
                    {{ $type->name }} {{ implode(', ', json_decode($type->role)) }}
                </option>
            @endif
        @endforeach
    </select>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search