一覧のURL https://domain/admin/novel_writings
実際に動かして無いので不具合あるかもですが、
下記それぞれのファイルの修正コードになります。
修正部分が多いので、ファイルの中身をそのまま貼り付けてます。
routes/web.php
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
/*
Route::get('/', function () {
return view('welcome');
});
*/
Route::group(['prefix' => 'admin', 'as'=> 'admin.', 'middleware' => 'auth'], function() {
Route::resource('novel_writings', 'Admin\NovelWritingController');
});
Route::get('/index', 'NovelWritingController@index');
Auth::routes();
Route::get('/', 'NovelWritingController@index');
app/Http/Controllers/Admin/NovelWritingController.php
<?php
namespace App\Http\Controllers\Admin;
use App\NovelWriting;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class NovelWritingController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$cond_title = $request->cond_title;
if ($cond_title != '') {
// 検索されたら検索結果を取得する
$posts = NovelWriting::where('title', $cond_title)->get();
}else{
// それ以外はすべてのニュースを取得する
$posts = NovelWriting::all();
}
return view('admin.novel_writings.index', ['posts' => $posts, 'cond_title' => $cond_title]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('admin.novel_writings.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
// 以下を追記
// Varidationを行う
$this->validate($request, NovelWriting::$rules);
$novelwriting = new NovelWriting;
$form = $request->all();
// フォームから送信されてきた_tokenを削除する
unset($form['_token']);
$novelwriting->fill($form);
$novelwriting->save();
return redirect()->route('admin.novel_writings.index');
}
/**
* Display the specified resource.
*
* @param \App\NovelWriting $novelWriting
* @return \Illuminate\Http\Response
*/
public function show(Request $request, NovelWriting $novelWriting)
{
$cond_title = $request->cond_title;
if ($cond_title != '') {
// 検索されたら検索結果を取得する
$posts = NovelWriting::where('title', $cond_title)->get();
}else{
// それ以外はすべてのニュースを取得する
$posts = NovelWriting::all();
}
return view('admin.novel_writings.index', ['admin' => User::findOrFail($id)]);
}
/**
* Show the form for editing the specified resource.
*
* @param \App\NovelWriting $novelWriting
* @return \Illuminate\Http\Response
*/
public function edit(NovelWriting $novelWriting)
{
return view('admin.novel_writings.edit', ['novelwriting_form' => $novelWriting]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\NovelWriting $novelWriting
* @return \Illuminate\Http\Response
*/
public function update(Request $request, NovelWriting $novelWriting)
{
// Validationをかける
$this->validate($request, NovelWriting::$rules);
// 送信されてきたフォームデータを格納する
$novelWriting_form = $request->all();
unset($novelWriting_form['remove']);
unset($novelWriting_form['_token']);
// 該当するデータを上書きして保存する
$novelWriting->fill($novelWriting_form)->save();
// $history = new History();
// $history->news_id = $news->id;
// $history->edited_at = Carbon::now();
// $history->save();
return redirect()->route('admin.novel_writings.index');
}
/**
* Remove the specified resource from storage.
*
* @param \App\NovelWriting $novelWriting
* @return \Illuminate\Http\Response
*/
public function destroy(NovelWriting $novelWriting)
{
// 削除する
$novelWriting->delete();
return redirect()->route('admin.novel_writings.index');
}
}
resources/views/admin/novel_writings/create.blade.php
@extends('layouts.admin')
@section('title', '小説の新規作成')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 mx-auto">
<h2>新規小説作成</h2>
<form action="{{ route('admin.novel_writings.store') }}" method="post" enctype="multipart/form-data">
@if (count($errors) > 0)
<ul>
@foreach($errors->all() as $e)
<li>{{ $e }}</li>
@endforeach
</ul>
@endif
<div class="form-group row">
<label class="col-md-2">タイトル</label>
<div class="col-md-10">
<input type="text" class="form-control" name="title" value="{{ old('title') }}">
</div>
</div>
<div class="form-group row">
<label class="col-md-2">章</label>
<div class="col-md-10">
<input type="text" class="form-control" name="chapter" value="{{ old('chapter') }}">
</div>
<div class="form-group row">
<label class="col-md-2">節</label>
<div class="col-md-10">
<input type="text" class="form-control" name="section" value="{{ old('section') }}">
</div>
</div>
<div class="form-group row">
<label class="col-md-2">本文</label>
<div class="col-md-10">
<textarea class="form-control" name="content" rows="20">{{ old('content') }}</textarea>
</div>
</div>
{{ csrf_field() }}
<input type="submit" class="btn btn-primary" value="保存">
</form>
</div>
</div>
</div>
@endsection
resources/views/admin/novel_writings/edit.blade.php
@extends('layouts.admin')
@section('title', '小説の編集')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 mx-auto">
<h2>小説編集</h2>
<form action="{{ route('admin.novel_writings.update', $novelwriting_form->id) }}" method="post" enctype="multipart/form-data">
@if (count($errors) > 0)
<ul>
@foreach($errors->all() as $e)
<li>{{ $e }}</li>
@endforeach
</ul>
@endif
<div class="form-group row">
<label class="col-md-2" for="title">タイトル</label>
<div class="col-md-10">
<input type="text" class="form-control" name="title" value="{{ $novelwriting_form->title }}">
</div>
</div>
<div class="form-group row">
<label class="col-md-2" for="content">本文</label>
<div class="col-md-10">
<textarea class="form-control" name="content" rows="20">{{ $novelwriting_form->content }}</textarea>
</div>
</div>
<div class="form-group row">
<div class="col-md-10">
<input type="hidden" name="id" value="{{ $novelwriting_form->id }}">
{{ csrf_field() }}
<input type="submit" class="btn btn-primary" value="更新">
</div>
</div>
</form>
{{-- 以下を追記
<div class="row mt-5">
<div class="col-md-4 mx-auto">
<h2>編集履歴</h2>
<ul class="list-group">
@if ($novelwriting_form->histories != NULL)
@foreach ($novelwriting_form->histories as $history)
<li class="list-group-item">{{ $history->edited_at }}</li>
@endforeach
@endif
</ul>
</div>
</div>
--}}
</div>
</div>
</div>
@endsection
resources/views/admin/novel_writings/index.blade.php
@extends('layouts.admin')
@section('title', '小説一覧')
@section('content')
<div class="container">
<div class="row">
<h2>作成済み小説の一覧</h2>
</div>
<div class="row">
<div class="col-md-4">
<a href="{{ route('admin.novel_writings.create') }}" role="button" class="btn btn-primary">新規作成</a>
</div>
<div class="col-md-8">
<form action="{{ route('admin.novel_writings.index') }}" method="get">
<div class="form-group row">
<label class="col-md-2">タイトル</label>
<div class="col-md-8">
<input type="text" class="form-control" name="cond_title" value={{ $cond_title }}>
</div>
<div class="col-md-2">
{{ csrf_field() }}
<input type="submit" class="btn btn-primary" value="検索">
</div>
</div>
</form>
</div>
</div>
<div class="row">
<div class="admin-index col-md-12 mx-auto">
<div class="row">
<table class="table table-dark">
<thead>
<tr>
<!--<th width="10%">ID</th>-->
<th width="20%">タイトル</th>
<!--<th width="50%">本文</th>-->
<!--<th width="10%">操作</th>-->
</tr>
</thead>
<tbody>
@foreach($posts as $novel)
<tr>
<th>{{ $novel->id }}</th>
<td>{{ \Str::limit($novel->title, 100) }}</td>
<td>{{ \Str::limit($novel->body, 250) }}</td>
<td>
<div>
<a href="{{ route('admin.novel_writings.edit', $novel->id) }}">編集</a>
</div>
<div>
<a href="{{ route('admin.novel_writings.destroy', $novel->id) }}">削除</a>
</div>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
@endsection