Создадим новую модель.
Перейдем в консоль и
напишем там:
php artisan make:model
Model/admin/Permission -m -r
Перенесем
PermissionController.php в папку Admin.
И поменяем в
контроллере namesapce:
namespace
App\Http\Controllers\Admin;
Откроем
миграцию:
\database\migrations\2017_09_10_200912_create_permissions_table.php
Создадим
две таблицы для БД.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePermissionsTable extends Migration
{
public function up()
{
Schema::create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Schema::create('permission_role', function (Blueprint $table) {
$table->integer('role_id');
$table->integer('permission_id');
});
}
public function down()
{
Schema::dropIfExists('permissions');
}
}
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePermissionsTable extends Migration
{
public function up()
{
Schema::create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Schema::create('permission_role', function (Blueprint $table) {
$table->integer('role_id');
$table->integer('permission_id');
});
}
public function down()
{
Schema::dropIfExists('permissions');
}
}
Перейдем
в консоль:
php
artisan migrate
Перейдем в
вид: \resources\views\admin\layouts\sidebar.blade.php
<li
class=""><a href="{{ route('permission.index')
}}"><i class="fa fa-circle-o"></i>
Permissions</a></li>
Создадим
маршрут \routes\web.php
Route::resource('admin/permission',
'PermissionController');
Идем в контроллер
\app\Http\Controllers\Admin\PermissionController.php
добавляем
строку
use
App\Http\Controllers\Controller;
public
function index()
{
$permissions = Permission::all();
return view('admin.permission.show', compact('permissions'));
}
{
$permissions = Permission::all();
return view('admin.permission.show', compact('permissions'));
}
Создадим вид.
Скопируем
папку role и переименуем
ее в permission.
Откроем
файл 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">Permissions</h3>
<a class="col-lg-offset-5 btn
btn-success" href="{{ route('permission.create') }}">Add
New Permission</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>Permission Name</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
@foreach ($permissions as $permission)
<tr>
<td>{{ $loop->index + 1
}}</td>
<td>{{ $permission->name
}}</td>
<td><a href="{{
route('permission.edit',$permission->id) }}"><span
class="glyphicon glyphicon-edit"></span></a></td>
<td>
<form id="delete-form-{{
$permission->id }}" method="post" action="{{
route('permission.destroy', $permission->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-{{
$permission->id }}').submit();
}else{
event.preventDefault();
}"><span class="glyphicon
glyphicon-trash"></span></a>
</td>
</tr>
@endforeach
</tbody>
<tfoot>
<tr>
<th>S.No</th>
<th>Permission 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
Идем в
\resources\views\admin\permission\create.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">Permission</h3>
</div>
<!-- /.box-header -->
@include('includes.messages')
<!-- form start -->
<form role="form" action="{{
route('permission.store') }}" method="post">
{{ csrf_field() }}
<div class="box-body">
<div class="col-lg-offset-3
col-lg-6">
<div class="form-group">
<label for="name">Permission
title</label>
<input type="text"
class="form-control" id="name" name="name"
placeholder="Permission Title">
</div>
<div class="form-group">
<button type="submit"
class="btn btn-primary">Submit</button>
<a type="button"
href="{{ route('permission.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\PermissionController.php
public
function create()
{
return view('admin.permission.create');
}
{
return view('admin.permission.create');
}
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|max:50|unique:permissions'
]);
$permission = new Permission;
$permission->name = $request->name;
$permission->save();
return redirect(route('permission.index'));
}
{
$this->validate($request, [
'name' => 'required|max:50|unique:permissions'
]);
$permission = new Permission;
$permission->name = $request->name;
$permission->save();
return redirect(route('permission.index'));
}
Реализуем
удаление.
public function destroy(Permission $permission)
{
Permission::where('id', $permission->id)->delete();
return redirect()->back();
}
}
{
Permission::where('id', $permission->id)->delete();
return redirect()->back();
}
}
Реализуем
редактирование.
public
function edit(Permission $permission)
{
$permission = Permission::find($permission->id);
return view('admin.permission.edit', compact('permission'));
}
{
$permission = Permission::find($permission->id);
return view('admin.permission.edit', compact('permission'));
}
Откроем вид
\resources\views\admin\permission\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 Permission</h3>
</div>
<!-- /.box-header -->
@include('includes.messages')
<!-- form start -->
<form role="form" action="{{
route('permission.update', $permission->id) }}"
method="post">
{{ csrf_field() }}
{{ method_field('PUT') }}
<div class="box-body">
<div class="col-lg-offset-3 col-lg-6">
<div class="form-group">
<label for="name">Permission
title</label>
<input type="text" class="form-control"
id="name" name="name" placeholder="Permission
Title" value="{{ $permission->name }}">
</div>
<div class="form-group">
<button type="submit" class="btn
btn-primary">Submit</button>
<a type="button" href="{{
route('permission.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\PermissionController.php
и реализуем
обновление.
public
function update(Request $request, Permission $permission)
{
$this->validate($request, [
'name' => 'required|max:50'
]);
$permission = Permission::find($permission->id);
$permission->name = $request->name;
$permission->save();
return redirect(route('permission.index'))->with('message', 'Permission updated successfully');
}
{
$this->validate($request, [
'name' => 'required|max:50'
]);
$permission = Permission::find($permission->id);
$permission->name = $request->name;
$permission->save();
return redirect(route('permission.index'))->with('message', 'Permission updated successfully');
}
Реализуем
вывод сообщений
\resources\views\includes\messages.blade.php
@if
(session()->has('message'))
<p class="alert alert-success">{{ session('message')
}}</p>
@endif
Для
вывода сообщений откроем файл
show.blade.php
<div
class="box-header">
@include('includes.messages')
</div>
Идем
в контроллер
\app\Http\Controllers\Admin\PermissionController.php
и добавим
сообщение об удалении
public
function destroy(Permission $permission)
{
Permission::where('id', $permission->id)->delete();
return redirect()->back()->with('message', 'Permission updated successfully');
}
}
{
Permission::where('id', $permission->id)->delete();
return redirect()->back()->with('message', 'Permission updated successfully');
}
}
Комментариев нет:
Отправить комментарий