前提・実現したいこと
現在Laravelでツイッタークローンにていいね機能を実装しようとしています。
favoritesテーブルを作りボタンの設置まで進めて動作確認をしたところ、他のユーザーの投稿に対してボタンは正常に反応するのですが、自分の投稿には反応せずエラーが出ます。色々試したのですが、まだまだ勉強を始めたばかりなものでいまいち解決方がわかりません。
お手数ですが何かしら解決策を教えていただけると幸いです。
発生している問題・エラーメッセージ
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`microposts`.`favorites`, CONSTRAINT `favorites_micropost_id_foreign` FOREIGN KEY (`micropost_id`) REFERENCES `users` (`id`) ON DELETE CASCADE) (SQL: insert into `favorites` (`created_at`, `micropost_id`, `updated_at`, `user_id`) values
該当のソースコード
PHP
1/_create_favorites_table.php 2 3public function up() 4 { 5 Schema::create('favorites', function (Blueprint $table) { 6 $table->increments('id'); 7 $table->integer('user_id')->unsigned()->inded(); 8 $table->integer('micropost_id')->unsigned()->inded(); 9 $table->timestamps(); 10 11 $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); 12 $table->foreign('micropost_id')->references('id')->on('users')->onDelete('cascade'); 13 14 $table->unique(['user_id','micropost_id']); 15 }); 16 } 17 18 19 public function down() 20 { 21 Schema::dropIfExists('favorites'); 22 } 23} 24
PHP
1/app/User.php 2 3 public function favorites() 4 { 5 return $this->belongsToMany(Micropost::class,'favorites','user_id','micropost_id')->withTimestamps(); 6 } 7 8 9 10 11 public function favorite($micropostId) 12 { 13 $exist =$this->is_favorites($micropostId); 14 15 if($exist){ 16 return false; 17 } 18 else{ 19 $this->favorites()->attach($micropostId); 20 return true; 21 } 22 } 23 24 public function unfavorite($micropostId) 25 { 26 $exist = $this->is_favorites($micropostId); 27 28 if($exist){ 29 $this->favorites()->detach($micropostId); 30 return true; 31 } 32 else{ 33 return false; 34 } 35 } 36 37 public function is_favorites($micropostId) 38 { 39 return $this->favorites()->where('micropost_id',$micropostId)->exists(); 40 } 41 42 43} 44
PHP
1/app/Microposts.php 2 3 4 public function favorite_users() 5 { 6 return $this->belongsToMany(User::class,'favorites','micropost_id','user_id')->withTimestamps(); 7 }
PHP
1/Controller/FavoritesController.php 2 3class FavoritesController extends Controller 4{ 5 public function store(Request $request, $id) 6 { 7 8 \Auth::user()->favorite($id); 9 return back(); 10 } 11 12 public function destroy($id) 13 { 14 \Auth::user()->unfavorite($id); 15 return back(); 16 } 17} 18
PHP
1/favorite_button.blade.php 2 3@if (Auth::user() -> is_favorites($micropost->id)) 4 {!! Form::open(['route' => ['favorites.unfavorite',$micropost->id],'method' => 'delete']) !!} 5 {!! Form::submit('Unfavorite',['class' => "btn btn-success btn-sm"]) !!} 6 {!! Form::close() !!} 7 @else 8 {!! Form::open(['route' => ['favorites.favorite',$micropost->id]]) !!} 9 {!! Form::submit('Favorite',['class' => "btn btn-light btn-sm"]) !!} 10 {!! Form::close() !!} 11 @endif
試したこと
favorite_buttonにある引数$micropostを$userにしてみました。→エラーは出ませんが一つのボタンで全て反応してしまいます。
なので$micropostに関係する部分に何か問題あるのかなと思うのですが、よく分かりません。。。
補足情報(FW/ツールのバージョンなど)
回答1件
あなたの回答
tips
プレビュー