laravel5.2で、ブログサイトを作っています。
ララ帳というサイトを見たりと、真似している状態です。
2点ほど、お聞きしたく思っています。
一は、編集ボタンを作ってみたのですがエラーがでてしまうこと。
もう一つは、ボタンのデザインが統一されないことです。また、<table class="table table-striped table-hover">とかを指定しても、反映されていないようなので、その辺りも教えていただければ助かります。
私自身、理解していない部分も多いですがよろしくお願いします。
app.blade.php <!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>L5Blog</title> <link href="/css/app.css" rel="stylesheet"> </head> <body> <div class="container"> <div class="row"> <div class="page-header"> <h1>L5Blog</h1> <div class="col-md-12"> @yield('content') </div> </div> </div> </body> </html>
index.blade.php @section('content') <div class="page-header"> <h2>記事一覧</h2> </div> <div> <a href='/articles/create' class="btn btn-primary btn-lg">投稿</a> </div> <table class="table table-striped table-hover"> <thead> <tr> <th>タイトル</th> <th>本文</th> <th>作成日時</th> <th>更新日時</th> </tr> </thead> <tbody> @foreach($articles as $article) <tr> <td>{{{ $article->title }}}</td> <td>{{{ $article->body }}}</td> <td>{{{ $article->created_at }}}</td> <td>{{{ $article->updated_at }}}</td> <!--詳細ページへのリンクを追加 --> <td> <a href="/articles/show/{{{ $article->id }}}" class="btn btn-primary btn-xs">詳細</a> <a href="/articles/edit/{{{ $article->id }}}" class="btn btn-success btn-xs">編集</a> {!! Form::open(['action' => ['ArticlesController@postDelete', $article->id]]) !!} <button type="submit" class="btn btn-danger btn-xs">削除</button> {!! Form::close() !!} </td> </tr> @endforeach </tbody> </table> @endsection
edit.blade.php @extends('app') @section('content') <h2 class="page-header">記事編集</h2> {!! Form::open(['action' => ['ArticlesController@postEdit', $article->id]]) !!} <div class="form-group"> <label>タイトル</label> {!! Form::input('text', 'title', $article->title, ['required', 'class' => 'form-control']) !!} </div> <div class="form-group"> <label>本文</label> {!! Form::textarea('body', $article->body, ['required', 'class' => 'form-control']) !!} </div> <button type="submit" class="btn btn-default">編集</button> {!! Form::close() !!} @endsection
show.blade.php @extends('app') @section('content') <h2 class="page-header">記事詳細</h2> <table class="table table-striped"> <li> <a href="/articles/edit/{{{ $article->id }}}" class="btn btn-primary pull-left"> 編集</a> </li> <li> {!! Form::open(['action' =>['ArticlesController@postDelete', $article->id]]) !!} <button type="submit" class="btn btn-danger pull-left">削除</button> {!! Form::close() !!} </li> </table> <tbody> <tr> <th>タイトル</th> <td>{{{ $article->title }}}</td> </tr> <tr> <th>本文</th> <td>{{{ $article->body }}}</td> </tr> <tr> <th>作成日時</th> <td>{{{ $article->created_at }}}</td> </tr> <tr> <th>更新日時</th> <td>{{{ $article->updated_at }}}</td> </tr> </tbody> </table> @endsection
エラー内容についてです。
編集ボタンを押すと以下のように表示されます。
ErrorException in UrlGenerator.php line 602:
Action App\Http\Controllers\ArticlesController@postEdit not defined. (View: C:\Users\Laravel_practice\resources\views\articles\edit.blade.php)
routes.phpとArticlesController.phpを追記しました。
cssフレームワークは、laravel5.2にbootstrapが入っているので、そちらを使いたいです。
度々、すみませんがよろしくお願いします。
routes.php ?php Route::get('/', 'ArticlesController@getIndex'); Route::controller('articles', 'ArticlesController');
ArticlesController.php <?php namespace App\Http\Controllers; use App\Article; use App\Http\Requests; use App\Http\Controllers\Controller; use Illuminate\Http\Request; class ArticlesController extends Controller { /** * @var Article * 一覧、詳細、作成、編集、削除のアクションをそれぞれ定義する */ protected $article; /** * @param Article $article */ public function __construct(Article $article) { $this-> article = $article; } /** * 記事の一覧 * * @return \Illuminate\View\View */ public function getIndex() { $articles = $this->article->all(); return view('articles.index')->with(compact('articles')); } /** * 記事の詳細 * * @param $id * @return \Illuminate\View\View */ public function getShow($id) { $article = $this->article->find($id); return view('articles.show',compact('article')); } /** * 記事の投稿 * GET/articles/createの場合はgetCreate() * POST/articles/createの場合はpostCreate() * @return \Illuminate\View\View */ public function getCreate() { return view('articles.create'); } /** * 記事の投稿 * * * @param Request $request * @return \Illuminate\Http\RedirectResponse */ public function postCreate(Request $request) { $data = $request->all(); $this->article->fill($data); $this->article->save(); return redirect()->to('articles/index'); } /** * 記事の編集 * * @param $id * @return \Illuminate\View\Viwe */ public function getEdit($id) { $article = $this->article->find($id); return view('articles.edit')->withArticle($article); } /** * 記事の編集 * * @param Request $request * @param $id * @return \Illuminate\Http\RedirectResponse */ /** * 記事の削除 * * @param $id * @return \Illuminate\Http\RedirectResponse */ public function postDelete($id) { $article = $this->article->find($id); $article->delete(); return redirect()->to('articles/index'); }
回答2件
あなたの回答
tips
プレビュー