お知らせ登録・編集・削除を実装
This commit is contained in:
parent
53ac4c9b8f
commit
be700ab81b
@ -5,6 +5,8 @@ namespace App\Http\Controllers\Admin;
|
||||
use App\Information;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class InfoController extends Controller
|
||||
{
|
||||
@ -24,7 +26,32 @@ class InfoController extends Controller
|
||||
|
||||
public function create()
|
||||
{
|
||||
// TODO
|
||||
return view('admin.info.create')->with([
|
||||
'categories' => Information::CATEGORIES
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$inputs = $request->all();
|
||||
if ($request->has('content')) {
|
||||
$inputs['content'] = str_replace(["\r\n", "\r"], "\n", $inputs['content']);
|
||||
}
|
||||
if (!$request->has('pinned')) {
|
||||
$inputs['pinned'] = false;
|
||||
}
|
||||
|
||||
// TODO: #updateと全く同じだし、フォームリクエストにしたほうがよいのでは?
|
||||
Validator::make($inputs, [
|
||||
'category' => ['required', Rule::in(array_keys(Information::CATEGORIES))],
|
||||
'pinned' => 'nullable|boolean',
|
||||
'title' => 'required|string|max:255',
|
||||
'content' => 'required|string|max:10000'
|
||||
])->validate();
|
||||
|
||||
$info = Information::create($inputs);
|
||||
|
||||
return redirect()->route('admin.info.edit', ['info' => $info])->with('status', 'お知らせを更新しました。');
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
@ -37,8 +64,32 @@ class InfoController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request)
|
||||
public function update(Request $request, Information $info)
|
||||
{
|
||||
// TODO
|
||||
$inputs = $request->all();
|
||||
if ($request->has('content')) {
|
||||
$inputs['content'] = str_replace(["\r\n", "\r"], "\n", $inputs['content']);
|
||||
}
|
||||
if (!$request->has('pinned')) {
|
||||
$inputs['pinned'] = false;
|
||||
}
|
||||
|
||||
Validator::make($inputs, [
|
||||
'category' => ['required', Rule::in(array_keys(Information::CATEGORIES))],
|
||||
'pinned' => 'nullable|boolean',
|
||||
'title' => 'required|string|max:255',
|
||||
'content' => 'required|string|max:10000'
|
||||
])->validate();
|
||||
|
||||
$info->fill($inputs)->save();
|
||||
|
||||
return redirect()->route('admin.info.edit', ['info' => $info])->with('status', 'お知らせを更新しました。');
|
||||
}
|
||||
|
||||
public function destroy(Information $info)
|
||||
{
|
||||
$info->delete();
|
||||
|
||||
return redirect()->route('admin.info')->with('status', 'お知らせを削除しました。');
|
||||
}
|
||||
}
|
||||
|
@ -16,5 +16,9 @@ class Information extends Model
|
||||
3 => ['label' => 'メンテナンス', 'class' => 'badge-warning']
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'category', 'pinned', 'title', 'content'
|
||||
];
|
||||
|
||||
protected $dates = ['deleted_at'];
|
||||
}
|
||||
|
46
resources/views/admin/info/create.blade.php
Normal file
46
resources/views/admin/info/create.blade.php
Normal file
@ -0,0 +1,46 @@
|
||||
@extends('layouts.admin')
|
||||
|
||||
@section('title', 'お知らせ')
|
||||
|
||||
@section('tab-content')
|
||||
<div class="container">
|
||||
<h2>お知らせの作成</h2>
|
||||
<hr>
|
||||
<form action="{{ route('admin.info.store') }}" method="post">
|
||||
{{ csrf_field() }}
|
||||
|
||||
<div class="row">
|
||||
<div class="form-group col-12 col-lg-6">
|
||||
<label for="category">カテゴリ</label>
|
||||
<select id="category" name="category" class="form-control">
|
||||
@foreach($categories as $id => $category)
|
||||
<option value="{{ $id }}" {{ old('category') == $id ? 'selected' : '' }}>{{ $category['label'] }}</option>
|
||||
@endforeach
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group col-12 col-lg-6 d-flex flex-column justify-content-center">
|
||||
<div class="custom-control custom-checkbox">
|
||||
<input id="pinned" name="pinned" type="checkbox" class="custom-control-input" value="1"
|
||||
{{ old('pinned') ? 'checked' : ''}}>
|
||||
<label for="pinned" class="custom-control-label">ピン留め (常に優先表示) する</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="title">タイトル</label>
|
||||
<input id="title" name="title" type="text" class="form-control" value="{{ old('title') }}">
|
||||
</div>
|
||||
<div class="form-group mt-3">
|
||||
<label for="content">本文</label>
|
||||
<textarea id="content" name="content" rows="15" class="form-control" maxlength="10000">{{ old('content') }}</textarea>
|
||||
<small class="form-text text-muted">
|
||||
最大 10000 文字、Markdown 形式
|
||||
</small>
|
||||
</div>
|
||||
|
||||
<div class="d-flex justify-content-between">
|
||||
<button type="submit" class="btn btn-primary">登録</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
@endsection
|
@ -6,7 +6,7 @@
|
||||
<div class="container">
|
||||
<h2>お知らせの編集</h2>
|
||||
<hr>
|
||||
<form action="{{ route('admin.info.update', ['id' => $info->id]) }}" method="post">
|
||||
<form action="{{ route('admin.info.update', ['info' => $info]) }}" method="post">
|
||||
{{ method_field('PUT') }}
|
||||
{{ csrf_field() }}
|
||||
|
||||
@ -15,13 +15,13 @@
|
||||
<label for="category">カテゴリ</label>
|
||||
<select id="category" name="category" class="form-control">
|
||||
@foreach($categories as $id => $category)
|
||||
<option value="{{ $id }}" {{ (old('category') ?? $info->category) == $id ? 'checked' : '' }}>{{ $category['label'] }}</option>
|
||||
<option value="{{ $id }}" {{ (old('category') ?? $info->category) == $id ? 'selected' : '' }}>{{ $category['label'] }}</option>
|
||||
@endforeach
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group col-12 col-lg-6 d-flex flex-column justify-content-center">
|
||||
<div class="custom-control custom-checkbox">
|
||||
<input id="pinned" type="checkbox" class="custom-control-input"
|
||||
<input id="pinned" name="pinned" type="checkbox" class="custom-control-input" value="1"
|
||||
{{ (is_bool(old('pinned')) ? old('pinned') : $info->pinned) ? 'checked' : ''}}>
|
||||
<label for="pinned" class="custom-control-label">ピン留め (常に優先表示) する</label>
|
||||
</div>
|
||||
@ -41,8 +41,12 @@
|
||||
|
||||
<div class="d-flex justify-content-between">
|
||||
<button type="submit" class="btn btn-primary">更新</button>
|
||||
<button type="button" class="btn btn-danger">削除</button>
|
||||
<button type="submit" class="btn btn-danger" form="delete-form">削除</button>
|
||||
</div>
|
||||
</form>
|
||||
<form id="delete-form" action="{{ route('admin.info.destroy', ['info' => $info]) }}" method="post">
|
||||
{{ method_field('DELETE') }}
|
||||
{{ csrf_field() }}
|
||||
</form>
|
||||
</div>
|
||||
@endsection
|
@ -53,6 +53,8 @@ Route::middleware('can:admin')
|
||||
Route::get('/', 'DashboardController@index')->name('dashboard');
|
||||
Route::get('/info', 'InfoController@index')->name('info');
|
||||
Route::get('/info/create', 'InfoController@create')->name('info.create');
|
||||
Route::get('/info/{id}', 'InfoController@edit')->name('info.edit');
|
||||
Route::put('/info/{id}', 'InfoController@update')->name('info.update');
|
||||
Route::post('/info', 'InfoController@store')->name('info.store');
|
||||
Route::get('/info/{info}', 'InfoController@edit')->name('info.edit');
|
||||
Route::put('/info/{info}', 'InfoController@update')->name('info.update');
|
||||
Route::delete('/info/{info}', 'InfoController@destroy')->name('info.destroy');
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user