teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

誤字を修正しました。

2021/03/13 11:38

投稿

ohshiro_hide
ohshiro_hide

スコア3

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,7 @@
1
1
  ### 前提・実現したいこと
2
2
  ブログの削除機能を実装したいと思っています。
3
3
 
4
- (例)PHP(laravel5.8)でブログのシステムを作っています。
4
+ PHP(laravel5.8)でブログのシステムを作っています。
5
5
  削除機能を実装中に以下のエラーメッセージが発生しました。
6
6
  具体的には、ブログ一覧画面の削除ボタンを押す→ブログ内容を表示(画面偏移)→そのページに設置している削除ボタンを押して完了、という形です。
7
7
 

1

list.blade.phpコードを追加しました。

2021/03/13 11:38

投稿

ohshiro_hide
ohshiro_hide

スコア3

title CHANGED
File without changes
body CHANGED
@@ -118,7 +118,63 @@
118
118
  }
119
119
 
120
120
  ```
121
+ ```
122
+ list.blade.php(ブログ一覧画面)
121
123
 
124
+ @extends('../layouts/app')
125
+ @section('title', 'ブログ')
126
+ @auth
127
+ @php
128
+ $title = __('You\'ve verified your email address.');
129
+ @endphp
130
+ @section('content')
131
+ <div class="row">
132
+ <div class="col-md-10 col-md-offset-2">
133
+ <h2>ブログ記事一覧</h2>
134
+ <button type="button" class="btn btn-primary" onclick="location. href='/blog/create'">新規登録</button>
135
+ @if(session('err_msg'))
136
+ <p class="text-danger">{{session('err_msg')}}</p>
137
+ @endif
138
+
139
+ <table class="table table-striped">
140
+ <tr>
141
+ <th>記事番号</th>
142
+ <th>タイトル</th>
143
+ <th>日付</th>
144
+ <th></th>
145
+ <th></th>
146
+ </tr>
147
+ @foreach($blogs as $blog)
148
+ <tr>
149
+ <td>{{ $blog->id }}</td>
150
+ <td><a href="/blog/{{ $blog->id }}">{{ $blog->title }}</a></td>
151
+ <td>{{ $blog->updated_at }}</td>
152
+ <td><button type="button" class="btn btn-primary" onclick="location. href='/blog/edit/{{ $blog->id }}'">編集</button></td>
153
+ <td><button type="button" class="btn btn-primary" onclick="location. href='/blog/checkdelete/{{ $blog->id }}'">削除</button></td>
154
+ <!-- <form method="post" action="{{ route('delete',$blog->id) }}" onSubmit="return checkDelete()">
155
+  @csrf
156
+ <td><button type="submit" class="btn btn-primary" onclick=>削除</button></td>
157
+ </form> -->
158
+ </tr>
159
+ @endforeach
160
+ </table>
161
+ </div>
162
+ <script>
163
+ function checkDelete(){
164
+ if(window.confirm('削除してよろしいですか?')){
165
+ return true;
166
+ } else {
167
+ return false;
168
+ }
169
+ }
170
+ </script>
171
+ @endsection
172
+ @endauth
173
+ @guest
174
+ <p style="color:blue">管理者用画面です。ログインしてください。</p>
175
+ @endguest
176
+
177
+ ```
122
178
  ### 試したこと
123
179
 
124
180
  最初はdelete.blade.phpでforeach文を使用しformを投稿内容と同じ数だけ作成したのですが機能しませんでした。調べてみると同ページにformが複数あるのは好ましくないとの記事を見たため、formではなくbuttonに変更してページを偏移させてみました。