воскресенье, 10 сентября 2017 г.

Laravel. Блог. Редактирование роли. Добавление пользователя

Сделаем ссылку на роли в сайдбаре.


Откроем \resources\views\admin\layouts\sidebar.blade.php
<li class=""><a href="{{ route('role.index') }}"><i class="fa fa-circle-o"></i> Roles</a></li>

Идем в контроллер \app\Http\Controllers\Admin\RoleController.php
public function edit($id)
{
// $role = role::where('id', $id)->first();
$role = role::find($id);
return view('admin.role.edit', compact('role'));
}

public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required|max:50'
]);
$role = role::find($id);
$role->name = $request->name;
$role->save();
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="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\UserController.php
Не забываем подключить:
use App\Model\admin\role;

public function create()
{
$roles = role::all();
return view('admin.user.create', compact('roles'));
}

Откроем \resources\views\admin\user\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">Add Admin</h3>
</div>
<!-- /.box-header -->
@include('includes.messages')
<!-- form start -->
<form role="form" action="{{ route('user.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">User Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="User Name">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" class="form-control" id="email" name="email" placeholder="Email">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password" placeholder="Password">
</div>
<div class="form-group">
<label for="confirm_password">Confirm Password</label>
<input type="password" class="form-control" id="confirm_password" name="confirm_password" placeholder="Confirm Password">
</div>
<div class="form-group">
<label>Assign Role</label>
<div class="row">
@foreach ($roles as $role)
<div class="col-lg-3">
<div class="checkbox">
<label><input type="checkbox" name="role[]" value="{{ $role->id }}"/>{{ $role->name }}</label>
</div>
</div>
@endforeach
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
<a type="button" href="{{ route('user.index') }}" class="btn btn-warning">Back</a>
</div>
</div>
</div>
<!-- /.box-body -->
</form>
<!-- /.box -->
</div>
<!-- /.col-->
</div>
<!-- ./row -->
</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->

@endsection

Обратите внимание, что name="role[]" дает массив!!!

В контроллере \app\Http\Controllers\Admin\UserController.php
сохраним данные:
public function store(Request $request)
{
return $request->all();
}

Жмем на Submit. У нас выводится:
_token "WHRqsVezWU6QruZryxBwMidyRDxvPwHSCm98puOQ"
name "Ce"
email "ce@mail.ru"
password "123456"
confirm_password "12345"
role
0 "1"
1 "2"

2 "3"

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

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

Materialize-css. Футер

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