LoginSignup
0
0

More than 1 year has passed since last update.

Laravel 6.xで削除機能

Posted at

自分用の削除機能の備忘録。
今回は削除画像を押下→「削除しますか?」という確認→OKで削除 の流れを実装します。

view

index.blade.php
<div class="postList">
   @foreach($posts as $post)
   <div class="postName">
     <p>{{ $users->username }}</p>
   </div>
   <div class="postListText">
     <p>{{ $post->post }}</p>
   </div>
   <div class="postListImage">
     <a href=""><img src="images/edit.png"></a>
     <div class="postListDeleteImage">
       <a class="postDelete" href="{{ $post->id }}/delete" onclick="return confirm('この投稿を削除します。よろしいでしょうか?')"><img src="images/trash.png"></a>
       <input type="image" name="postdelete2" class="postDelete2" src="images/trash-h.png">
     </div>
   </div>
   @endforeach
 </div>

Controller

PostsController.php
public function delete($id){
     \DB::table('posts')
       ->where('id', $id)
       ->delete();
     return redirect('/top');
}

ルーティング

web.php
Route::get('/{id}/delete', 'PostsController@delete');

これでいけました

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