0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Laravelにおける投稿、削除 --備忘録

Posted at

前に書いたやつの続きです。備忘録であります。

1 新規投稿、保存

(フォームに記事を投稿できなければ、意味は)ないです。よね。そこで新規投稿機能の作製を行いましょう。初めに、新規投稿なので...

contolloer/articleのcreateに記載

 public function create(Request $request)
 {
     $message = 'New data';
     return view('new', ['message' => $message]);
 }

と記載を行います。ここでは一旦受け取るデータをダミーの'new data';としておきます。

新規投稿画面が欲しいのでreworses/new.blade.phpに

    <h1>paiza bbs</h1>
     <p>{{ $message }}</p>
     {{ Form::open(['route' => 'article.store']) }}
         <div class='form-group'>
             {{ Form::label('content', 'Content:') }}
             {{ Form::text('content', null) }}
         </div>
        <div class='form-group'>
            {{ Form::label('user_name', 'Name:') }}
            {{ Form::text('user_name', null) }}
        </div>
         <div class="form-group">
             {{ Form::submit('作成する', ['class' => 'btn btn-primary']) }}
             <a href={{ route('article.list') }}>一覧に戻る</a>
       </div>
     {{ Form::close() }}

と記載します

これでは固定テキストしか表示されません。やっぱ自分で書いたやつ、欲しいですよね。

contolloerに

    Route::post('/arcile', 'ArticleController@store')->name('article.store');

と追加します。これで

 {{ Form::open(['route' => 'article.store']) }}

にルーティングが設定されました。ではArticleController のstoreを書いていきましょう。

htttp/Contolloer/Arcicleclonntollortに

 public function store(Request $request)
{
     $article = new Article;
     $article->content = $request->content;
     $article->user_name =  $request ->user_name;
     $article->save();
     return redirect()->route('article.show', ['id' => $article->id]);
 }

と記入します。returnのreturn redirect()->route....は
同じくhttp/contolloer/Articlecontolloerにある

http/contolloer/Articlecontolloer

public function show(Request $request, $id, Article $article)
 {
     $message = 'This is your article ' . $id;
     $article = Article::find($id);
     return view('show', ['message' => $message, 'article' => $article]);
 }

を呼び出すってわけですね。ここで両者のコントローラーを見てみましょう。

routes/web.phpより、

    Route::post('/arcile', 'ArticleController@store')->name('article.store');
    Route::get('/article/{id}', 'ArticleController@show')->name('article.show');

両者ともに最終的にはview のshow に偏移しますが、前者の@Stoerはnameを受け取るほんの少しの遠回りをしったってことです。
次に編集フォームを編集していきます。

resources/views/show.blade.php
     <h1>paiza bbs</h1>
     <p>{{ $message }}</p>
     <p>{{ $article->content }}</p>
     <p>{{ $article->user_name }}</p>

     <p>
         <a href={{ route('article.list') }} class='btn btn-outline-primary'>一覧に戻る</a>
         <a href={{ route('article.edit', ["id" =>  $article->id]) }} class='btn btn-outline-primary'>編集</a>
     </p>
     <div>
         {{ Form::open(['method' => 'delete', 'route' => ['article.delete', $article->id]]) }}
             {{ Form::submit('削除', ['class' => 'btn btn-outline-secondary']) }}
         {{ Form::close() }}
     </div>

投稿したら、投稿した奴だけを移したいので(XXXXが投稿されました。としたい)

     public function edit(Request $request, $id, Article $article)
     {
         $message = 'Edit your article ' . $id;
         $article = Article::find($id);
         return view('edit', ['message' => $message, 'article' => $article]);
    }

と記入。vieweditとする事で、それ専用のページを作ります
//以下view/edit

 <h1>paiza bbs</h1>
 <p>{{ $message }}</p>
 {{ Form::model($article, ['route' => ['article.update', $article->id]]) }}
     <div class='form-group'>
         {{ Form::label('content', 'Content:') }}
         {{ Form::text('content', null) }}
     </div>
     <div class='form-group'>
         {{ Form::label('user_name', 'Name:') }}
         {{ Form::text('user_name', null) }}
     </div>
     <div class="form-group">
         {{ Form::submit('保存する', ['class' => 'btn btn-primary']) }}
         <a href={{ route('article.show', ['id' => $article->id]) }}>戻る</a>
    </div>
 {{ Form::close() }}

これでよかです。やったぜ。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?