Добавим выбор тегов
и категории при редактировании постов.
Идем в
\resources\views\admin\post\edit.blade.php
@extends('admin.layouts.app')
@section('headSection')
<link
rel="stylesheet" href="{{
asset('admin/bower_components/select2/dist/css/select2.min.css') }}"
/>
@endsection
@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>
@include('includes.messages')
<!--
/.box-header -->
<!--
form start -->
<form
role="form" action="{{ route('post.update', $post->id)
}}" method="post" enctype="multipart/form-data">
{{
csrf_field() }}
{{
method_field('PATCH') }}
<div
class="box-body">
<div class="col-lg-6">
<div class="form-group">
<label for="title">Post title</label>
<input type="text" class="form-control"
id="title" name="title" placeholder="Title"
value="{{ $post->title }}">
</div>
<div class="form-group">
<label for="subtitle">Post Sub Title</label>
<input type="text" class="form-control"
id="subtitle" name="subtitle" placeholder="Sub
Title" value="{{ $post->subtitle }}">
</div>
<div class="form-group">
<label for="slug">Post Slug</label>
<input type="text" class="form-control"
id="slug" name="slug" placeholder="Post
Slug" value="{{ $post->slug }}">
</div>
</div>
<div class="col-lg-6">
<br />
<div class="form-group">
<div class="pull-right">
<label for="image">File input</label>
<input type="file" name="image"
id="image">
</div>
<div class="checkbox pull-left">
<label>
<input type="checkbox" name="status"
@if ($post->status == 1) checked @endif> Publish
</label>
</div>
</div>
<br />
<div class="form-group" style="margin-top: 18px">
<label>Select Tags</label>
<select class="form-control select2"
multiple="multiple" data-placeholder="Select a State"
style="width: 100%;">
<option>Alabama</option>
<option>Alaska</option>
<option>California</option>
<option>Delaware</option>
<option>Tennessee</option>
<option>Texas</option>
<option>Washington</option>
</select>
</div>
<div class="form-group" style="margin-top: 18px">
<label>Select Category</label>
<select class="form-control select2"
multiple="multiple" data-placeholder="Select a State"
style="width: 100%;">
<option>Alabama</option>
<option>Alaska</option>
<option>California</option>
<option>Delaware</option>
<option>Tennessee</option>
<option>Texas</option>
<option>Washington</option>
</select>
</div>
</div>
</div>
<!--
/.box-body -->
<div
class="box">
<div class="box-header">
<h3 class="box-title">Write Post Body
<small>Simple and fast</small>
</h3>
</div>
<div class="col-md-6">
<!-- /.box-header -->
<div class="box-body pad">
<textarea class="textarea" placeholder="Place some
text here" name="body"
style="width: 100%; height: 500px; font-size: 14px;
line-height: 18px; border: 1px solid #dddddd; padding: 10px;">{{
$post->body }}</textarea>
</div>
</div>
<div
class="box-footer">
<button type="submit" class="btn
btn-primary">Submit</button>
<a
type="button" href="{{ route('post.index') }}"
class="btn btn-warning">Back</a>
</div>
</form>
</div>
<!--
/.box -->
</div>
<!--
/.col-->
</div>
<!--
./row -->
</section>
<!--
/.content -->
</div>
<!--
/.content-wrapper -->
@endsection
@section('footerSection')
<script
src="{{
asset('admin/bower_components/select2/dist/js/select2.full.min.js')
}}"></script>
<script>
$(document).ready(function() {
$('.select2').select2();
});
</script>
@endsection
Идем в
\resources\views\admin\post\post.blade.php
@extends('admin.layouts.app')
@section('headSection')
<link
rel="stylesheet" href="{{
asset('admin/bower_components/select2/dist/css/select2.min.css') }}"
/>
@endsection
@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>
@include('includes.messages')
<!--
/.box-header -->
<!--
form start -->
<form
role="form" action="{{ route('post.store') }}"
method="post" enctype="multipart/form-data">
{{
csrf_field() }}
<div
class="box-body">
<div class="col-lg-6">
<div class="form-group">
<label for="title">Post
title</label>
<input type="text"
class="form-control" id="title" name="title"
placeholder="Title">
</div>
<div class="form-group">
<label for="subtitle">Post
Sub Title</label>
<input type="text"
class="form-control" id="subtitle"
name="subtitle" placeholder="Sub Title">
</div>
<div class="form-group">
<label for="slug">Post
Slug</label>
<input type="text"
class="form-control" id="slug" name="slug"
placeholder="Post Slug">
</div>
</div>
<div class="col-lg-6">
<br />
<div class="form-group">
<div class="pull-right">
<label for="image">File
input</label>
<input type="file"
name="image" id="image">
</div>
<div class="checkbox pull-left">
<label>
<input type="checkbox"
name="status" value="1"> Publish
</label>
</div>
</div>
<br />
<div class="form-group"
style="margin-top: 18px">
<label>Select Tags</label>
<select class="form-control select2"
multiple="multiple" data-placeholder="Select a State"
style="width: 100%;">
<option>Alabama</option>
<option>Alaska</option>
<option>California</option>
<option>Delaware</option>
<option>Tennessee</option>
<option>Texas</option>
<option>Washington</option>
</select>
</div>
<div class="form-group"
style="margin-top: 18px">
<label>Select Category</label>
<select class="form-control select2"
multiple="multiple" data-placeholder="Select a State"
style="width: 100%;">
<option>Alabama</option>
<option>Alaska</option>
<option>California</option>
<option>Delaware</option>
<option>Tennessee</option>
<option>Texas</option>
<option>Washington</option>
</select>
</div>
</div>
</div>
<!--
/.box-body -->
<div
class="box">
<div class="box-header">
<h3 class="box-title">Write Post
Body
<small>Simple and fast</small>
</h3>
</div>
<!-- /.box-header -->
<div class="box-body pad">
<textarea class="textarea"
placeholder="Place some text here" name="body"
style="width: 100%; height: 500px;
font-size: 14px; line-height: 18px; border: 1px solid #dddddd;
padding: 10px;"></textarea>
</div>
</div>
<div
class="box-footer">
<button type="submit" class="btn
btn-primary">Submit</button>
<a
type="button" href="{{ route('post.index') }}"
class="btn btn-warning">Back</a>
</div>
</form>
</div>
<!--
/.box -->
</div>
<!--
/.col-->
</div>
<!--
./row -->
</section>
<!--
/.content -->
</div>
<!--
/.content-wrapper -->
@endsection
@section('footerSection')
<script
src="{{
asset('admin/bower_components/select2/dist/js/select2.full.min.js')
}}"></script>
<script>
$(document).ready(function() {
$('.select2').select2();
});
</script>
@endsection
Идем в
\app\Http\Controllers\Admin\PostController.php
use
App\Model\user\category;
use App\Model\user\tag;
use App\Model\user\tag;
public
function create()
{
$tags = tag::all();
$categories = category::all();
return view('admin.post.post', compact('tags', 'categories'));
}
{
$tags = tag::all();
$categories = category::all();
return view('admin.post.post', compact('tags', 'categories'));
}
public
function edit($id)
{
$post = post::where('id', $id)->first();
$tags = tag::all();
$categories = category::all();
return view('admin.post.edit', compact('post', 'tags', 'categories'));
}
{
$post = post::where('id', $id)->first();
$tags = tag::all();
$categories = category::all();
return view('admin.post.edit', compact('post', 'tags', 'categories'));
}
Выведем теги
и категории в цикле.
Идем в
\resources\views\admin\post\post.blade.php
@extends('admin.layouts.app')
@section('headSection')
<link rel="stylesheet" href="{{
asset('admin/bower_components/select2/dist/css/select2.min.css') }}"
/>
@endsection
@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>
@include('includes.messages')
<!-- /.box-header -->
<!-- form start -->
<form role="form" action="{{
route('post.store') }}" method="post"
enctype="multipart/form-data">
{{ csrf_field() }}
<div class="box-body">
<div class="col-lg-6">
<div class="form-group">
<label for="title">Post
title</label>
<input type="text"
class="form-control" id="title" name="title"
placeholder="Title">
</div>
<div class="form-group">
<label for="subtitle">Post
Sub Title</label>
<input type="text"
class="form-control" id="subtitle"
name="subtitle" placeholder="Sub Title">
</div>
<div class="form-group">
<label for="slug">Post
Slug</label>
<input type="text"
class="form-control" id="slug" name="slug"
placeholder="Post Slug">
</div>
</div>
<div class="col-lg-6">
<br />
<div class="form-group">
<div class="pull-right">
<label for="image">File
input</label>
<input type="file"
name="image" id="image">
</div>
<div class="checkbox
pull-left">
<label>
<input
type="checkbox" name="status" value="1">
Publish
</label>
</div>
</div>
<br />
<div class="form-group"
style="margin-top: 18px">
<label>Select Tags</label>
<select class="form-control
select2" multiple="multiple" data-placeholder="Select
a State"
style="width:
100%;" name="tags[]>
@foreach($tags as $tag)
<option value="{{
$tag->id }}">{{ $tag->name}}</option>
@endforeach
</select>
</div>
<div class="form-group"
style="margin-top: 18px">
<label>Select
Category</label>
<select class="form-control
select2" multiple="multiple" data-placeholder="Select
a State"
style="width:
100%;" name="categories[]">
@foreach($categories as
$category)
<option value="{{
$category->id }}">{{ $category->name}}</option>
@endforeach
</select>
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box">
<div class="box-header">
<h3 class="box-title">Write
Post Body
<small>Simple and fast</small>
</h3>
</div>
<!-- /.box-header -->
<div class="box-body pad">
<textarea class="textarea"
placeholder="Place some text here" name="body"
style="width: 100%;
height: 500px; font-size: 14px; line-height: 18px; border: 1px solid
#dddddd; padding: 10px;"></textarea>
</div>
</div>
<div class="box-footer">
<button type="submit"
class="btn btn-primary">Submit</button>
<a type="button" href="{{
route('post.index') }}" class="btn btn-warning">Back</a>
</div>
</form>
</div>
<!-- /.box -->
</div>
<!-- /.col-->
</div>
<!-- ./row -->
</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
@endsection
@section('footerSection')
<script src="{{
asset('admin/bower_components/select2/dist/js/select2.full.min.js')
}}"></script>
<script>
$(document).ready(function() {
$('.select2').select2();
});
</script>
@endsection
Вставляем
тоже в \resources\views\admin\post\edit.blade.php
<div
class="form-group" style="margin-top: 18px">
<label>Select Tags</label>
<select class="form-control select2"
multiple="multiple" data-placeholder="Select a State"
style="width: 100%;"
name="tags[]">
@foreach($tags as $tag)
<option value="{{ $tag->id }}">{{
$tag->name}}</option>
@endforeach
</select>
</div>
<div class="form-group" style="margin-top:
18px">
<label>Select Category</label>
<select class="form-control select2"
multiple="multiple" data-placeholder="Select a State"
style="width: 100%;"
name="categories[]">
@foreach($categories as $category)
<option value="{{ $category->id
}}">{{ $category->name}}</option>
@endforeach
</select>
</div>
Сохраним в БД
названия категорий и тегов.
Идем в
\app\Http\Controllers\Admin\PostController.php
public function update(Request $request, $id)
{
$this->validate($request, [
'title'=>'required',
'subtitle'=>'required',
'slug'=>'required',
'body'=>'required'
]);
$post = post::find($id);
$post->title = $request->title;
$post->subtitle = $request->subtitle;
$post->slug = $request->slug;
$post->body = $request->body;
$post->status= $request->status;
$post->tags()->sync($request->tags);
$post->categories()->sync($request->categories);
$post->save();
return redirect(route('post.index'));
}
{
$this->validate($request, [
'title'=>'required',
'subtitle'=>'required',
'slug'=>'required',
'body'=>'required'
]);
$post = post::find($id);
$post->title = $request->title;
$post->subtitle = $request->subtitle;
$post->slug = $request->slug;
$post->body = $request->body;
$post->status= $request->status;
$post->tags()->sync($request->tags);
$post->categories()->sync($request->categories);
$post->save();
return redirect(route('post.index'));
}
public function store(Request $request)
{
$this->validate($request, [
'title'=>'required',
'subtitle'=>'required',
'slug'=>'required',
'body'=>'required'
]);
$post = new post;
$post->title = $request->title;
$post->subtitle = $request->subtitle;
$post->slug = $request->slug;
$post->body = $request->body;
$post->status= $request->status;
$post->save();
$post->tags()->sync($request->tags);
$post->categories()->sync($request->categories);
return redirect(route('post.index'));
}
{
$this->validate($request, [
'title'=>'required',
'subtitle'=>'required',
'slug'=>'required',
'body'=>'required'
]);
$post = new post;
$post->title = $request->title;
$post->subtitle = $request->subtitle;
$post->slug = $request->slug;
$post->body = $request->body;
$post->status= $request->status;
$post->save();
$post->tags()->sync($request->tags);
$post->categories()->sync($request->categories);
return redirect(route('post.index'));
}
Теперь при
редактировании поста нужно, чтобы статус
published отображался галочкой.
Дописываем в
\resources\views\admin\post\edit.blade.php
<div
class="checkbox pull-left">
<label>
<input type="checkbox"
name="status" value="1" @if ($post->status ==
1) {{'checked'}} @endif> Publish
</label>
</div>
Комментариев нет:
Отправить комментарий