我有一个费用管理系统项目,其中有一些复选框,我想将每个复选框保存为行并给他们一个ID,因为我想使用这些复选框进行访问,因此我被困在那里我从表单中获取值并将该值保存在数据库中。
https://i.imgur.com/kY6jsNL.png
我有5张桌子 1:用户
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('role_id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->string('status');
$table->string('image');
$table->rememberToken();
$table->timestamps();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
});
2:角色
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('permission')->nullable();
$table->timestamps();
});
3:模块
Schema::create('modules', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
4:访问名
Schema::create('access_names', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
5:模块权限
Schema::create('module_permissions', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedInteger('user_id');
$table->unsignedInteger('role_id');
$table->unsignedBigInteger('access_id');
$table->unsignedBigInteger('module_id');
$table->enum('status', ['0', '1']);
$table->timestamps();
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
$table->foreign('role_id')
->references('id')->on('roles')
->onDelete('cascade');
$table->foreign('access_id')
->references('id')->on('access_names')
->onDelete('cascade');
$table->foreign('module_id')
->references('id')->on('modules')
->onDelete('cascade');
});
这些是我要在“模块权限”中存储数据的表,因此我无法在此DB中保存数据,因为我在控制器中以数组形式获取数据以及如何保存表单数据。 这是一些细节,这是我的模块,这是我的访问名。这些都是固定的,永远不会增加
https://i.imgur.com/pG2NDVf.png
这是我将数据发送到名称为
的控制器的表单Permission.blade.php
@foreach($roles as $role)
<form class="form-horizontal module-permission" name="name_{{$role->id}}" action="{{route('store.module.permission')}}" method="post">
<br/>
@php
$user_id = DB::table('users')->where('role_id',$role->id)->pluck('id');
@endphp
@csrf
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapse_{{$role->id}}"><i class="fa fa-bars"></i> Permission For : {{$role->name}} </a>
</h4>
</div>
<div id="collapse_{{$role->id}}" class="panel-collapse collapse ">
<div class="panel-body">
<table class="table table-bordered dt-responsive rolesPermissionTable">
<thead>
<tr class="showRolesPermission">
<th scope="col">Modules</th>
<th scope="col">Create</th>
<th scope="col">Read</th>
<th scope="col">Update</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
<input type="hidden" name="data['Role'][{{$role->id}}]" value="{{$role->id}}" />
<input type="hidden" name="data['User']{{$user_id}}" value={{$user_id}} />
@foreach($module_permissions as $module_permission)
@if($module_permission->id == 1)
<tr>
<th scope="col" colspan="5" class="showRolesPermission text-center">
<input type="hidden" name="data['Expense'][1]" value="1" />Expenses
</th>
</tr>
@endif
@if($module_permission->id == 2)
<tr>
<th scope="col" colspan="5" class="showRolesPermission text-center">
<input type="hidden" name="data['ExpenseCategory'][2]" value="2" />Expenses Category
</th>
</tr>
@endif
@if($module_permission->id == 3)
<tr>
<th scope="col" colspan="5" class="showRolesPermission text-center">
<input type="hidden" name="data['Income'][3]" value="3" />Incomes
</th>
</tr>
@endif
@if($module_permission->id == 4)
<tr>
<th scope="col" colspan="5" class="showRolesPermission text-center">
<input type="hidden" name="data['IncomesCategory'][4]" value="4" />Incomes Category
</th>
</tr>
@endif
<tr>
<th scope="row" class="thfont">Own Entries</th>
@foreach($access_names as $access_name)
<td><input type="checkbox" name="{{'data['.$module_permission->id.']['.$access_name->id.']'}}" value="1" /></td>
@endforeach
</tr>
<tr>
<th scope="row" class="thfont">All Entries</th>
<td>-</td>
@foreach($access_names as $access_name)
@if($access_name->id != 1)
<td><input type="checkbox" name="{{'data['.$module_permission->id.']['.$access_name->id.']'}}" value="1" /></td>
@endif
@endforeach
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
<hr>
<input type="submit" name="save" value="Save Permission" class="btn btn-wide btn-success margin-top-20" />
</form>@endforeach
这是我的脚本代码,我将这些数据发送到控制器。
$('.module-permission').submit(function (event) {
var form = $('form')[0]; // You need to use standard javascript object here
var formData = new FormData(form);
$.ajax({
url: "{{route('store.module.permission')}}",
data: formData,
type: 'POST',
contentType: false, // NEEDED, DON'T OMIT THIS (requires jQuery 1.6+)
processData: false, // NEEDED, DON'T OMIT THIS
success: function () {// ... Other options like success and etc
}
});
});
这是我的控制器 我应该在控制器中写些什么,以便将数据保存在“模块权限”表中。
ModulePermissionController.php
public function store(Request $request)
{
$module_permission = new ModulePermission();
$data1 = $request->data;
dd($data1);
}
我正在像这样在控制器中获取数据。
https://i.imgur.com/xvWfCJ7.png
我获得了自己条目的数据,但没有获得所有条目的数据,我为OWN和ALL都赋予了相同的名称,即name =“ Data [这里是权限ID] [,这是访问名称],其值为“ 1”;
赞
https://i.imgur.com/Sqi5SA4.png
现在我的第一个问题是这是我如何同时获取属于自己的条目和所有条目的数据,以及如何在控制器中调整这些数据,以便将这些数据轻松插入数据库中。 插入后,我很容易从数据库中获取数据。因为我要从图片中显示的此复选框向用户授予权限。 提前谢谢!