前提・実現したいこと
ポートフォリオを作成中です。
記事を削除する機能が動作しなくなりました。
問題なく削除できるようにしたいです。
発生している問題・エラーメッセージ
403 This action is unauthorized.
と表示されます。
該当のソースコード
該当箇所の一部を記載しています。
追記する必要があればコメントをお願いします。
ArticleController.php
PHP
1class ArticleController extends Controller 2{ 3 public function __construct() 4 { 5 $this->authorizeResource(Article::class, 'article'); 6 } 7 8 public function destroy(Article $article) 9 { 10 $article->delete(); 11 return redirect()->route('articles.index'); 12 } 13}
index.blade.php
PHP
1 @if( Auth::id() === $article->user_id ) 2 <div class="ml-auto card-text"> 3 <div class="dropup"> 4 <a data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> 5 <i class="fas fa-ellipsis-h"></i> 6 </a> 7 <div class="dropdown-menu dropdown-menu-right"> 8 <a class="dropdown-item" href="{{ route("articles.edit", ['article' => $article]) }}"> 9 <i class="fas fa-pen mr-1"></i>記事を更新する 10 </a> 11 <div class="dropdown-divider"></div> 12 <a class="dropdown-item text-danger" data-toggle="modal" data-target="#modal-delete-{{ $article->id }}"> 13 <i class="fas fa-trash-alt mr-1"></i>記事を削除する 14 </a> 15 </div> 16 </div> 17 </div> 18 19 <div id="modal-delete-{{ $article->id }}" class="modal fade" tabindex="-1" role="dialog"> 20 <div class="modal-dialog" role="document"> 21 <div class="modal-content"> 22 <div class="modal-header"> 23 <button type="button" class="close" data-dismiss="modal" aria-label="閉じる"> 24 <span aria-hidden="true">×</span> 25 </button> 26 </div> 27 <form method="POST" action="{{ route('articles.destroy', ['article' => $article]) }}"> 28 @csrf 29 @method('DELETE') 30 <div class="modal-body text-center"> 31 記事を削除します。よろしいですか? 32 </div> 33 <div class="modal-footer justify-content-between"> 34 <a class="btn btn-outline-grey" data-dismiss="modal">キャンセル</a> 35 <button type="submit" class="btn btn-danger">削除する</button> 36 </div> 37 </form> 38 </div> 39 </div> 40 </div> 41 @endif
ArticlePolicy.php
PHP
1 public function delete(User $user, Article $article) 2 { 3 $user->id === $article->user_id; 4 }
ArticleRequest.php
PHP
1 public function authorize() 2 { 3 return true; 4 }
試したこと
(1)
自身の投稿を削除したが、プログラムでは他人の投稿を削除する処理になっており、ポリシーでfalseを返したので、ステータスコード403が表示されていると予想しました。
試しにゲストユーザーを作成し、一つ投稿しました。
usersテーブルのidと、articlesテーブルのuser_idが一致しているかsqlで確認しました。
mysql> SELECT * from users;
+----+-----------------------+-----------------+-------------------+--------------------------------------------------------------+----------------+---------------------+---------------------+ | id | name | email | email_verified_at | password | remember_token | created_at | updated_at | +----+-----------------------+-----------------+-------------------+--------------------------------------------------------------+----------------+---------------------+---------------------+ | 1 | ゲストユーザー | guest@guest.com | NULL | $2y$10$JtAqHI1SxY3sE/7YgTiJ4u986SpK5CeNmIvQ2vWFRkRx2lMloTcZ6 | NULL | 2021-07-03 06:28:01 | 2021-07-03 06:28:01 | +----+-----------------------+-----------------+-------------------+--------------------------------------------------------------+----------------+---------------------+---------------------+ 1 row in set (0.00 sec)
mysql> SELECT * from articles;
+----+-------+------------+-----------+---------+---------------------+---------------------+ | id | image | title | body | user_id | created_at | updated_at | +----+-------+------------+-----------+---------+---------------------+---------------------+ | 1 | NULL | test title | test body | 1 | 2021-07-03 06:28:34 | 2021-07-03 06:28:34 | +----+-------+------------+-----------+---------+---------------------+---------------------+ 1 row in set (0.00 sec)
同じですね......
(2)
$user->idと$article->user_idはそれぞれ型が違うのかなと思ったので、ArticlePolicy.phpの$user->id === $article->user_id;
を$user->id == $article->user_id;
にしてみました。
結果は同じで、403 This action is unauthorized.
が表示されました。
一応SQLで確認。
mysql> show columns from users;
+-------------------+-----------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------------+-----------------+------+-----+---------+----------------+ | id | bigint unsigned | NO | PRI | NULL | auto_increment | | name | varchar(255) | NO | UNI | NULL | | | email | varchar(255) | NO | UNI | NULL | | | email_verified_at | timestamp | YES | | NULL | | | password | varchar(255) | NO | | NULL | | | remember_token | varchar(100) | YES | | NULL | | | created_at | timestamp | YES | | NULL | | | updated_at | timestamp | YES | | NULL | | +-------------------+-----------------+------+-----+---------+----------------+ 8 rows in set (0.04 sec)
mysql> show columns from articles;
+------------+-----------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+-----------------+------+-----+---------+----------------+ | id | bigint unsigned | NO | PRI | NULL | auto_increment | | image | varchar(255) | YES | | NULL | | | title | varchar(255) | NO | | NULL | | | body | text | NO | | NULL | | | user_id | bigint unsigned | NO | | NULL | | | created_at | timestamp | YES | | NULL | | | updated_at | timestamp | YES | | NULL | | +------------+-----------------+------+-----+---------+----------------+ 7 rows in set (0.01 sec)
どちらもbigint unsignedですね。
(3)
ArticlePolicy.phpの$user->id === $article->user_id;
をコメントアウトして、再度削除を実行しました。
403 This action is unauthorized.
が表示されました。
補足情報(FW/ツールのバージョンなど)
PHP 7.4.1
Laravel 6.20.26
回答1件
あなたの回答
tips
プレビュー