2019-02-11 23:25:46 +09:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
2019-03-14 00:05:11 +09:00
|
|
|
|
use App\Http\Controllers\Controller;
|
2019-02-11 23:25:46 +09:00
|
|
|
|
use App\Information;
|
|
|
|
|
use Illuminate\Http\Request;
|
2019-03-14 00:04:53 +09:00
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
|
use Illuminate\Validation\Rule;
|
2019-02-11 23:25:46 +09:00
|
|
|
|
|
|
|
|
|
class InfoController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function index()
|
|
|
|
|
{
|
|
|
|
|
$informations = Information::query()
|
|
|
|
|
->select('id', 'category', 'pinned', 'title', 'created_at')
|
|
|
|
|
->orderByDesc('pinned')
|
|
|
|
|
->orderByDesc('created_at')
|
|
|
|
|
->paginate(20);
|
|
|
|
|
|
|
|
|
|
return view('admin.info.index')->with([
|
|
|
|
|
'informations' => $informations,
|
|
|
|
|
'categories' => Information::CATEGORIES
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function create()
|
|
|
|
|
{
|
2019-03-14 00:04:53 +09:00
|
|
|
|
return view('admin.info.create')->with([
|
|
|
|
|
'categories' => Information::CATEGORIES
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function store(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$inputs = $request->all();
|
|
|
|
|
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', 'お知らせを更新しました。');
|
2019-02-11 23:25:46 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function edit($id)
|
|
|
|
|
{
|
|
|
|
|
$information = Information::findOrFail($id);
|
|
|
|
|
|
|
|
|
|
return view('admin.info.edit')->with([
|
|
|
|
|
'info' => $information,
|
|
|
|
|
'categories' => Information::CATEGORIES
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-14 00:04:53 +09:00
|
|
|
|
public function update(Request $request, Information $info)
|
2019-02-11 23:25:46 +09:00
|
|
|
|
{
|
2019-03-14 00:04:53 +09:00
|
|
|
|
$inputs = $request->all();
|
|
|
|
|
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', 'お知らせを削除しました。');
|
2019-02-11 23:25:46 +09:00
|
|
|
|
}
|
|
|
|
|
}
|