From be700ab81b662a8459237f1bf9572a235b69a531 Mon Sep 17 00:00:00 2001 From: shibafu Date: Thu, 14 Mar 2019 00:04:53 +0900 Subject: [PATCH] =?UTF-8?q?=E3=81=8A=E7=9F=A5=E3=82=89=E3=81=9B=E7=99=BB?= =?UTF-8?q?=E9=8C=B2=E3=83=BB=E7=B7=A8=E9=9B=86=E3=83=BB=E5=89=8A=E9=99=A4?= =?UTF-8?q?=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Admin/InfoController.php | 57 ++++++++++++++++++- app/Information.php | 4 ++ resources/views/admin/info/create.blade.php | 46 +++++++++++++++ resources/views/admin/info/edit.blade.php | 12 ++-- routes/web.php | 6 +- 5 files changed, 116 insertions(+), 9 deletions(-) create mode 100644 resources/views/admin/info/create.blade.php diff --git a/app/Http/Controllers/Admin/InfoController.php b/app/Http/Controllers/Admin/InfoController.php index 6a1c7c7..744da19 100644 --- a/app/Http/Controllers/Admin/InfoController.php +++ b/app/Http/Controllers/Admin/InfoController.php @@ -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', 'お知らせを削除しました。'); } } diff --git a/app/Information.php b/app/Information.php index e0ae6ed..2b237fb 100644 --- a/app/Information.php +++ b/app/Information.php @@ -16,5 +16,9 @@ class Information extends Model 3 => ['label' => 'メンテナンス', 'class' => 'badge-warning'] ]; + protected $fillable = [ + 'category', 'pinned', 'title', 'content' + ]; + protected $dates = ['deleted_at']; } diff --git a/resources/views/admin/info/create.blade.php b/resources/views/admin/info/create.blade.php new file mode 100644 index 0000000..4bdb53e --- /dev/null +++ b/resources/views/admin/info/create.blade.php @@ -0,0 +1,46 @@ +@extends('layouts.admin') + +@section('title', 'お知らせ') + +@section('tab-content') +
+

お知らせの作成

+
+
+ {{ csrf_field() }} + +
+
+ + +
+
+
+ + +
+
+
+
+ + +
+
+ + + + 最大 10000 文字、Markdown 形式 + +
+ +
+ +
+
+
+@endsection \ No newline at end of file diff --git a/resources/views/admin/info/edit.blade.php b/resources/views/admin/info/edit.blade.php index ff3e588..dc56cea 100644 --- a/resources/views/admin/info/edit.blade.php +++ b/resources/views/admin/info/edit.blade.php @@ -6,7 +6,7 @@

お知らせの編集


-
+ {{ method_field('PUT') }} {{ csrf_field() }} @@ -15,13 +15,13 @@
- pinned) ? 'checked' : ''}}>
@@ -41,8 +41,12 @@
- +
+
+ {{ method_field('DELETE') }} + {{ csrf_field() }} +
@endsection \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index 6f9bfb4..23eca8e 100644 --- a/routes/web.php +++ b/routes/web.php @@ -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'); });