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?

More than 3 years have passed since last update.

How to make delete button in laravel (File Upload Site #3)

Last updated at Posted at 2020-09-06
  1. ルートを作成
web.php
Route::delete('files/{id}', 'FilesController@destroy')->name('deletefile');
  1. View ファイルにフォームを追加する
home.blade.php
<th>
<form action="{{ route('deletefile', $file->id) }}" method="POST">
@csrf @method('DELETE')
<button type="submit" class="btn btn-outline-danger btn-delete"><i class="fa fa-trash"></i>Delete</button>
</form>
</th>

@csrf はクロスサイトリクエストフォージェリからの防蟻です。

3.コントローラーにfunction追加

filescontroller.php
public function destroy($id)
    {
        $del = File::find($id);
        Storage::delete($del->path);
        $del->delete();
        return redirect('/home');
    }

Screen Shot 2020-09-06 at 15.21.01.png
こんな感じです。

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?