mardi 21 avril 2020

How can I use same view file for different role types in Laravel 7?

I am creating a module to ToDo list. If the role type is admin, she/he can assign ToDo to any user. However, if a normal user creates a ToDo, it will automatically, be assigned to current user. I implemented like below:

ToDoController function looks like below:

public function create()
    {

        return view('todo.create',['countries' => Country::all(), 'business_types' => BusinessType::all(), 'users' => User::all()]);
    }

The view file create.blade.php with user selection select box is as below:

            @if(!empty(Auth::user()->roles()->where('name','admin')->first()))
                  <div class="form-group">
                    <label for="description">Assigned To</label>
                    <select class="form-control" name="user_id">
                        @foreach ($users as $user)
                          @if ($user->id == old('user_id'))
                            <option value="" selected></option>
                          @else
                            <option value=""></option>
                          @endif                                  
                        @endforeach
                    </select>
                    @if ($errors->has('user_id'))
                      <div class="alert-danger">
                        <p></p>
                      </div>
                    @endif
                  </div>
                @else
                  <div class="form-group" style="display:none;">
                    <label for="description">Assigned To</label>
                    <select class="form-control" name="user_id">
                        <option value="" selected></option>
                    </select>
                    @if ($errors->has('user_id'))
                      <div class="alert-danger">
                        <p></p>
                      </div>
                    @endif
                  </div>
                @endif

In the case of normal user, it is not required to take all users value, but I can't avoid that. Is there any optimized method to implement the same this ?

Thanks in Advance ! Manu



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2Kq0Rru
via IFTTT

Aucun commentaire:

Enregistrer un commentaire