понедельник, 11 сентября 2017 г.

Laravel. Блог. Связь: роль-разрешение

Идем \resources\views\admin\role\create.blade.php
копируем блоки с разрешением и переносим в \resources\views\admin\role\edit.blade.php


Добавим в файлы create и edit в input name="permission[]".

@extends('admin.layouts.app')

@section('main-content')
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Text Editors
<small>Advanced form element</small>
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
<li><a href="#">Forms</a></li>
<li class="active">Editors</li>
</ol>
</section>

<!-- Main content -->
<section class="content">
<div class="row">
<div class="col-md-12">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Edit Role</h3>
</div>
<!-- /.box-header -->
@include('includes.messages')
<!-- form start -->
<form role="form" action="{{ route('role.update', $role->id) }}" method="post">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<div class="box-body">
<div class="col-lg-offset-3 col-lg-6">
<div class="form-group">
<label for="name">Role title</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Role Title" value="{{ $role->name }}">
</div>
<div class="row">
<div class="col-lg-4">
<label for="name">Posts Permissions</label>
@foreach ($permissions as $permission)
@if ($permission->for == 'post')
<div class="checkbox">
<label><input type="checkbox" name="permission[]" value="{{ $permission->id }}"/>{{ $permission->name }}</label>
</div>
@endif
@endforeach
</div>
<div class="col-lg-4">
<label for="name">User Permissions</label>
@foreach ($permissions as $permission)
@if ($permission->for == 'user')
<div class="checkbox">
<label><input type="checkbox" name="permission[]" value="{{ $permission->id }}"/>{{ $permission->name }}</label>
</div>
@endif
@endforeach
</div>
<div class="col-lg-4">
<label for="name">Other Permissions</label>
@foreach ($permissions as $permission)
@if ($permission->for == 'other')
<div class="checkbox">
<label><input type="checkbox" name="permission[]" value="{{ $permission->id }}"/>{{ $permission->name }}</label>
</div>
@endif
@endforeach
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
<a type="button" href="{{ route('role.index') }}" class="btn btn-warning">Back</a>
</div>
</div>
</div>
<!-- /.box-body -->
</form>
<!-- /.box -->
</div>
<!-- /.col-->
</div>
<!-- ./row -->
</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->

@endsection

Откроем контроллер \app\Http\Controllers\Admin\RoleController.php
public function edit($id)
{
$role = role::find($id);
$permissions = Permission::all();
return view('admin.role.edit', compact('role', 'permissions'));
}

Создадим связь между ролями и разрешениями. Для этого откроем модель \app\Model\admin\role.php

<?php

namespace App\Model\admin;

use Illuminate\Database\Eloquent\Model;

class role extends Model
{
public function permissions() {
return $this->belongsToMany('App\Model\admin\Permission');
}

}


Откроем контроллер \app\Http\Controllers\Admin\RoleController.php
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required|max:50'
]);
$role = role::find($id);
$role->name = $request->name;
$role->save();
$role->permissions()->sync($request->permission);
return redirect(route('role.index'));
}

Идем в вид resources\views\admin\role\edit.blade.php
@extends('admin.layouts.app')

@section('main-content')
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Text Editors
<small>Advanced form element</small>
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
<li><a href="#">Forms</a></li>
<li class="active">Editors</li>
</ol>
</section>

<!-- Main content -->
<section class="content">
<div class="row">
<div class="col-md-12">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Edit Role</h3>
</div>
<!-- /.box-header -->
@include('includes.messages')
<!-- form start -->
<form role="form" action="{{ route('role.update', $role->id) }}" method="post">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<div class="box-body">
<div class="col-lg-offset-3 col-lg-6">
<div class="form-group">
<label for="name">Role title</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Role Title" value="{{ $role->name }}">
</div>
<div class="row">
<div class="col-lg-4">
<label for="name">Posts Permissions</label>
@foreach ($permissions as $permission)
@if ($permission->for == 'post')
<div class="checkbox">
<label><input type="checkbox" name="permission[]" value="{{ $permission->id }}"
@foreach ($role->permissions as $role_permit)
@if ($role_permit->id == $permission->id)
checked
@endif
@endforeach
/>{{ $permission->name }}</label>
</div>
@endif
@endforeach
</div>
<div class="col-lg-4">
<label for="name">User Permissions</label>
@foreach ($permissions as $permission)
@if ($permission->for == 'user')
<div class="checkbox">
<label><input type="checkbox" name="permission[]" value="{{ $permission->id }}"
@foreach ($role->permissions as $role_permit)
@if ($role_permit->id == $permission->id)
checked
@endif
@endforeach
/>{{ $permission->name }}</label>
</div>
@endif
@endforeach
</div>
<div class="col-lg-4">
<label for="name">Other Permissions</label>
@foreach ($permissions as $permission)
@if ($permission->for == 'other')
<div class="checkbox">
<label><input type="checkbox" name="permission[]" value="{{ $permission->id }}"
@foreach ($role->permissions as $role_permit)
@if ($role_permit->id == $permission->id)
checked
@endif
@endforeach
/>{{ $permission->name }}</label>
</div>
@endif
@endforeach
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
<a type="button" href="{{ route('role.index') }}" class="btn btn-warning">Back</a>
</div>
</div>
</div>
<!-- /.box-body -->
</form>
<!-- /.box -->
</div>
<!-- /.col-->
</div>
<!-- ./row -->
</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->

@endsection

Идем в вид пользователей и делаем исправления \resources\views\admin\user\show.blade.php

@extends('admin.layouts.app')

@section('headSection')
<link rel="stylesheet" href="{{ asset('admin/bower_components/datatables.net-bs/css/dataTables.bootstrap.min.css') }}" />
@endsection

@section('main-content')
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Blank page
<small>it all starts here</small>
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
<li><a href="#">Examples</a></li>
<li class="active">Blank page</li>
</ol>
</section>

<!-- Main content -->
<section class="content">

<!-- Default box -->
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">Users</h3>
<a class="col-lg-offset-5 btn btn-success" href="{{ route('user.create') }}">Add New User</a>

<div class="box-tools pull-right">
<button type="button" class="btn btn-box-tool" data-widget="collapse" data-toggle="tooltip"
title="Collapse">
<i class="fa fa-minus"></i></button>
<button type="button" class="btn btn-box-tool" data-widget="remove" data-toggle="tooltip" title="Remove">
<i class="fa fa-times"></i></button>
</div>
</div>
<div class="box-body">
<div class="box">
<div class="box-header">
<h3 class="box-title">Data Table With Full Features</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>S.No</th>
<th>User Name</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
@foreach ($users as $user)
<tr>
<td>{{ $loop->index + 1 }}</td>
<td>{{ $user->name }}</td>
<td><a href="{{ route('user.edit', $user->id) }}"><span class="glyphicon glyphicon-edit"></span></a></td>
<td>
<form id="delete-form-{{ $user->id }}" method="post" action="{{ route('user.destroy', $user->id) }}" style="display:none">
{{ csrf_field() }}
{{ method_field('DELETE') }}
</form>
<a href="" onclick="if(confirm('Are you sure, you want to delete this?'))
{event.preventDefault();
document.getElementById('delete-form-{{ $user->id }}').submit();
}else{
event.preventDefault();
}"><span class="glyphicon glyphicon-trash"></span></a>
</td>
</tr>
@endforeach
</tbody>
<tfoot>
<tr>
<th>S.No</th>
<th>User Name</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</tfoot>
</table>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
<!-- /.box-body -->
<div class="box-footer">
Footer
</div>
<!-- /.box-footer-->
</div>
<!-- /.box -->

</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
@endsection

@section('footerSection')
<script src="{{ asset('admin/bower_components/datatables.net/js/jquery.dataTables.min.js') }}"></script>
<script src="{{ asset('admin/bower_components/datatables.net-bs/js/dataTables.bootstrap.min.js') }}"></script>
<script>
$(function () {
$('#example1').DataTable()
})
</script>
@endsection

Идем в контроллер \app\Http\Controllers\Admin\UserController.php

public function edit($id)
{
return view('admin.user.edit');
}

Комментариев нет:

Отправить комментарий

Materialize-css. Футер

Сделаем футер и прижмем к низу страницы. Документация: https://materializecss.com/footer.html