суббота, 2 сентября 2017 г.

Laravel. Блог. Связи между таблицами

Реализуем удаление и редактирование тегов и категорий.
Откроем resources\views\admin\tag\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">Tags</h3>
<a class="col-lg-offset-5 btn btn-success" href="{{ route('tag.create') }}">Add New Tag</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>Tag Name</th>
<th>Slug</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
@foreach ($tags as $tag)
<tr>
<td>{{ $loop->index + 1 }}</td>
<td>{{ $tag->name }}</td>
<td>{{ $tag->slug }}</td>
<td><a href="{{ route('tag.edit', $tag->id) }}"><span class="glyphicon glyphicon-edit"></span></a></td>
<td>
<form id="delete-form-{{ $tag->id }}" method="post" action="{{ route('tag.destroy', $tag->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-{{ $tag->id }}').submit();
}else{
event.preventDefault();
}"><span class="glyphicon glyphicon-trash"></span></a>
</td>
</tr>
@endforeach
</tbody>
<tfoot>
<tr>
<th>S.No</th>
<th>Tag Name</th>
<th>Slug</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\category\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">Category create</h3>
<a class="col-lg-offset-5 btn btn-success" href="{{ route('category.create') }}">Add New Category</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-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>S.No</th>
<th>Tag Name</th>
<th>Slug</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
@foreach ($categories as $category)
<tr>
<td>{{ $loop->index + 1 }}</td>
<td>{{ $category->name }}</td>
<td>{{ $category->slug }}</td>
<td><a href="{{ route('category.edit', $category->id) }}"><span class="glyphicon glyphicon-edit"></span></a></td>
<td>
<form id="delete-form-{{ $category->id }}" method="post" action="{{ route('category.destroy', $category->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-{{ $category->id }}').submit();
}else{
event.preventDefault();
}"><span class="glyphicon glyphicon-trash"></span></a>
</td>
</tr>
@endforeach
</tbody>
<tfoot>
<tr>
<th>S.No</th>
<th>Tag Name</th>
<th>Slug</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</tfoot>
</table>
</div>
</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\CategoryController.php
public function destroy($id)
{
category::where('id', $id)->delete();
return redirect()->back();
}


\app\Http\Controllers\Admin\TagController.php
public function destroy($id)
{
tag::where('id', $id)->delete();
return redirect()->back();
}

Теперь сделаем редактирование.
\app\Http\Controllers\Admin\TagController.php
public function edit($id)
{
$tag = tag::where('id', $id)->first();
return view('admin.tag.edit', compact('tag'));
}

\app\Http\Controllers\Admin\CategoryController.php
public function edit($id)
{
$category = category::where('id', $id)->first();
return view('admin.category.edit', compact('category'));
}

Создадим шаблон для редактирования категории \resources\views\admin\category\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">Categories</h3>
</div>
<!-- /.box-header -->
@include('includes.messages')
<!-- form start -->
<form role="form" action="{{ route('category.update', $category->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">Category title</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Tag Title" value="{{ $category->name }}">
</div>
<div class="form-group">
<label for="slug">Category Slug</label>
<input type="text" class="form-control" id="slug" name="slug" placeholder="Post Slug" value="{{ $category->slug }}">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
<a type="button" href="{{ route('category.index') }}" class="btn btn-warning">Back</a>
</div>
</div>
</div>
</form>
<!-- /.box -->
</div>
<!-- /.col-->
</div>
<!-- ./row -->
</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->

@endsection

Обновление категории \app\Http\Controllers\Admin\CategoryController.php
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required',
'slug' => 'required',
]);

$category = category::find($id);
$category->name = $request->name;
$category->slug = $request->slug;
$category->save();
return redirect(route('category.index'));
}

Создадим шаблон для редактирования тегов \resources\views\admin\tag\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">Titles</h3>
</div>
<!-- /.box-header -->
@include('includes.messages')
<!-- form start -->
<form role="form" action="{{ route('tag.update', $tag->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">Tag title</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Tag Title" value="{{ $tag->name }}">
</div>
<div class="form-group">
<label for="slug">Tag Slug</label>
<input type="text" class="form-control" id="slug" name="slug" placeholder="Post Slug" value="{{ $tag->slug }}">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
<a type="button" href="{{ route('tag.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\TagController.php
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required',
'slug' => 'required',
]);

$tag = tag::find($id);
$tag->name = $request->name;
$tag->slug = $request->slug;
$tag->save();
return redirect(route('tag.index'));
}

Сделаем связи между таблицами в БД.
Соединим пост и тег. Один пост может иметь много тегов.
\app\Model\user\post.php
<?php

namespace App\Model\user;

use Illuminate\Database\Eloquent\Model;

class post extends Model
{
public function tags() {
return $this->belongsToMany('App\Model\user\tag', 'post_tags'); // post_tags - имя таблицы
}

public function categories() {
return $this->belongsToMany('App\Model\user\category', 'category_posts');
}
}


\app\Model\user\tag.php
<?php

namespace App\Model\user;

use Illuminate\Database\Eloquent\Model;

class tag extends Model
{
public function posts() {
return $this->belongsToMany('App\Model\user\post', 'post_tags');
}
}


\app\Model\user\category.php

<?php

namespace App\Model\user;

use Illuminate\Database\Eloquent\Model;

class category extends Model
{
public function posts() {
return $this->belongsToMany('App\Model\user\post', 'category_posts');
}
}

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

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

Materialize-css. Футер

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