0
votes

php files that represents the data needed to be added in the Pivot Table.

First I have my Roles.blade.php which contains the Roles of the Users:

@extends('layouts.loggedin')


@section('content')

<h1 class="text-center">Members Reports</h1>

<div class="row justify-content-center">

  <div class="col-md-10">

    <div class="card">
      <div class="card-header">Users</div>
      <div class="card-body">

        <table class="table">
  <thead>
    <tr>
      <th>#</th>
      <th>Role</th>

      <th>Action</th>
    </tr>
  </thead>
  <tbody>
    @foreach($roles as $role)
    <tr>
      <td>{{$role->id}}</td>
      <td>{{$role->name}}</td>

      <td>

        @can('view', \App\Model::class)
              <a href="/model/index">
                <button type="submit" class="btn btn-primary">Edit</button>
               </a>
         @endcan
        <button type="submit" class="btn btn-primary">Delete</button></td>
  </tr>

    @endforeach

  </tbody>
</table>


      </div>

    </div>


  </div>

</div>

@endsection

Second I have my The blade.php that is Linked to my Edit Button

@extends('layouts.loggedin')


@section('content')

<h1 class="text-center">Members Reports</h1>

<div class="row justify-content-center">

  <div class="col-md-10">

    <div class="card">
      <div class="card-header">Edit Abilities </div>
      <div class="card-body">

        <form class="" action="/members/update" method="post">
          @csrf


          @foreach ($models as $model)
          <div class="form-check">
            <label>{{$model->name}}</label>
            @foreach ($abilities as $ability)
            <div class="form-check">
              <input type="checkbox" name="abilities[]" value="{{$ability->id}}">
              <label>{{$ability->name}}</label>
            </div>
            @endforeach
          </div>
          @endforeach




          <!-- @can('update', App\User::class) -->
          <div class="form-group">
            <button type="submit" name="add" class="btn btn-success float-right">Update Abilities</button>
          </div>
          <!-- @endcan -->

        </form>


      </div>

    </div>


  </div>

</div>
@endsection

I have a pivot table called ability_model that represents the ability ( view, edit, update, delete ) on each model (Question, Answer, User, Report) and I have another pivot table called ability_model_role that should represent the Role ( admin or user ) assigned to the ability on the model and that should be added from that form.

1

1 Answers

0
votes

I think that this help you:

tables:

ability
 id      name
  1       view
  2       edit

ability_model
 id        ability    model
  1          1         2
  2          2         2


role:
id      name
 1      Admin
 2      User

ability_model_role
 role_id          ability_model_id
  1                      1
  1                      2
  2                      1

in your blade, you show model and his abilities

you can save the ability_model_id and role_id in ability_model_role

EDIT:

you can create a relationship from model to ability_model and after to do this:

Entity model:

   public function abilities_model()
    {
        return $this->hasMany(Abilites_model::class)
    }

in your blade:

 @foreach ($models as $model)
  <div class="form-check">
    <label>{{$model->name}}</label>
    @foreach ($model->abilities_model as $ability_model)
    <div class="form-check">
      <input type="checkbox" name="abilities[]" value="{{$ability_model->id}}">
      <label>{{$ability_model->name}}</label>
    </div>
    @endforeach
  </div>
  @endforeach
  

here you get ability_model->id

you should will to add a input select with roles for get role_id