вторник, 12 сентября 2017 г.

Laravel. Блог. Регистрация пользователя.

Регистрация пользователя.


Идем в модель \app\Model\admin\admin.php
<?php

namespace App\Model\admin;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class admin extends Authenticatable
{
use Notifiable;

protected $fillable = [
'name', 'email', 'password', 'status', 'phone'
];

protected $hidden = [
'password', 'remember_token',
];
}

Идем в вид создания нового пользователя \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" value="{{ old('name') }}">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" class="form-control" id="email" name="email" placeholder="Email" value="{{ old('email') }}">
</div>
<div class="form-group">
<label for="phone">Phone</label>
<input type="text" class="form-control" id="phone" name="phone" placeholder="Phone" value="{{ old('phone') }}">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password" placeholder="Password" value="{{ old('password') }}">
</div>
<div class="form-group">
<label for="confirm_password">Confirm Password</label>
<input type="password" class="form-control" id="confirm_password" name="password_confirmation" placeholder="Confirm Password">
</div>
<div class="form-group">
<label for="confirm_password">Status</label>
<div class="checkbox">
<label><input type="checkbox" name="status"
@if (old('status') == 1)
checked
@endif
value="1"/>Status</label>
</div>
</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

В виде отображения пользователей добавим вывод сообщений \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>
@include('includes.messages')

<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
<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Model\admin\admin;
use App\Model\admin\role;

class UserController extends Controller
{
public function __construct()
{
$this->middleware('auth:admin');
}


public function index()
{
$users = Admin::all();
return view('admin.user.show', compact('users'));
}


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


public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:admins',
'phone' => 'required|numeric',
'password' => 'required|string|min:6|confirmed',
]);
$request['password'] = bcrypt($request->password);
$user = admin::create($request->all());
return redirect(route('user.index'));
}



public function show($id)
{
//
}


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



public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255',
'phone' => 'required|numeric',
]);

$user = admin::where('id', $id)->update($request->except('_token','_method'));
return redirect(route('user.index'))->with('message', 'User has been updated successfully');
}



public function destroy($id)
{
admin::where('id', $id)->delete();
return redirect()->back()->with('message', 'User has been deleted successfully');
}

}


Создадим страницу редактирования пользователя \resources\views\admin\user\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">Update Admin</h3>
</div>
<!-- /.box-header -->
@include('includes.messages')
<!-- form start -->
<form role="form" action="{{ route('user.update',$user->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">User Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="User Name" value="{{ $user->name }}">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" class="form-control" id="email" name="email" placeholder="Email" value="{{ $user->email }}">
</div>
<div class="form-group">
<label for="phone">Phone</label>
<input type="text" class="form-control" id="phone" name="phone" placeholder="Phone" value="{{ $user->phone }}">
</div>
<div class="form-group">
<label for="confirm_password">Status</label>
<div class="checkbox">
<label><input type="checkbox" name="status"
@if ($user->status == 1)
checked
@endif
value="1"/>Status</label>
</div>
</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




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

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

Materialize-css. Футер

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