Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What are the problem?

posted at

Laravel 6.xで削除機能

自分用の削除機能の備忘録。
今回は削除画像を押下→「削除しますか?」という確認→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');

これでいけました

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
0
Help us understand the problem. What are the problem?