質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Laravel 5

Laravel 5は、PHPフレームワークLaravelの最新バージョンで、2014年11月に発表予定です。ディレクトリ構造がが現行版より大幅に変更されるほか、メソッドインジェクションやFormRequestの利用が可能になります。

Q&A

0回答

887閲覧

twitter風WEBアプリのフォロー機能の実装

hina0823

総合スコア15

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Laravel 5

Laravel 5は、PHPフレームワークLaravelの最新バージョンで、2014年11月に発表予定です。ディレクトリ構造がが現行版より大幅に変更されるほか、メソッドインジェクションやFormRequestの利用が可能になります。

0グッド

0クリップ

投稿2020/10/04 05:52

初めて投稿させていただきます。
現在、laravelを使いtwitter風WEBアプリを作成しています。
フォロー機能のところで躓き解決できません。
アドバイスを頂けると思い質問させていただきます。
######前提・実現したいこと


フォロー、フォロー解除の実装。
フォローボタンを押すと現れるエラーの解決。

######エラーメッセージ


ErrorException (E_WARNING)
Illegal offset type

フォローボタンを押すとこのようなエラーが出ます。
######該当ソースコード


search.blade.php

php

1@if (auth()->user()->isFollowed($user->id)) 2 <div class="px-2"> 3 <span class="px-1 bg-secondary text-light">フォローされています</span> 4 </div> 5 @endif 6 <div class="d-flex justify-content-end flex-grow-1"> 7 @if (auth()->user()->isFollowing($user->id)) 8 <form action="{{ route('unfollow', ['id' => $user->id]) }}" method="POST"> 9 {{ csrf_field() }} 10 {{ method_field('DELETE') }} 11 <button type="submit" class="btn btn-danger">フォロー解除</button> 12 </form> 13 @else 14 <form action="{{ route('follow', ['id' => $user->id]) }}" method="POST"> 15 {{ csrf_field() }} 16 <button type="submit" class="btn btn-primary">フォロー</button> 17 </form> 18 @endif

UsersController.php

php

1// フォロー 2 public function follow(User $user,$id) 3 { 4 $user = User::find($id); 5 $follower = auth()->user(); 6 $is_following = $follower->isFollowing($user->id); 7 if(!$is_following) { 8 $follower->follow($user->id); 9 return back(); 10 } 11 } 12 13// フォロー解除 14 public function unfollow(User $user,$id) 15 { 16 $user = User::find($id); 17 $follower = auth()->user(); 18 $is_following = $follower->isFollowing($user->id); 19 if($is_following) { 20 $follower->unfollow($user->id); 21 return back(); 22 } 23 }

User.php

php

1public function followers() 2 { 3 return $this->belongsToMany(self::class, 'follows', 'followed_id', 'following_id'); 4 } 5 6 public function follows() 7 { 8 return $this->belongsToMany(self::class, 'follows', 'following_id', 'followed_id'); 9 } 10 11 // フォローする 12 public function follow(Int $user_id) 13 { 14 return $this->follows()->attach($user_id); 15 } 16 17 // フォロー解除する 18 public function unfollow(Int $user_id) 19 { 20 return $this->follows()->detach($user_id); 21 } 22 23 // フォローしているか 24 public function isFollowing(Int $user_id) 25 { 26 return (boolean) $this->follows()->where('followed_id', $user_id)->exists(); 27 } 28 29 // フォローされているか 30 public function isFollowed(Int $user_id) 31 { 32 return (boolean) $this->followers()->where('following_id', $user_id)->exists(); 33 }

web.php

php

1Route::group(['middleware' => 'Auth'], function (){ 2Route::post('users/{user}/follow', 'UsersController@follow')->name('follow'); 3Route::delete('users/{user}/follow', 'UsersController@unfollow')->name('unfollow'); 4});

テーブル

php

1public function up() 2 { 3 Schema::create('follows', function (Blueprint $table) 4 { 5 $table->increments('id')->autoIncrement(); 6 $table->integer('following_id')->unsigned(); 7 $table->integer('followed_id')->unsigned(); 8 $table->timestamp('created_at')->useCurrent(); 9 // 外部キー制約 10 $table->foreign('following_id')->references('id')->on('users')->onDelete('cascade'); 11 $table->foreign('followed_id')->references('id')->on('users')->onDelete('cascade'); 12 // 組み合わせのダブりを禁止 13 $table->unique(['following_id', 'followed_id']); 14 }); 15 }

var_dump($user);

object(App\User)[233] protected 'fillable' => array (size=3) 0 => string 'username' (length=8) 1 => string 'mail' (length=4) 2 => string 'password' (length=8) protected 'hidden' => array (size=2) 0 => string 'password' (length=8) 1 => string 'remember_token' (length=14) protected 'connection' => string 'mysql' (length=5) protected 'table' => null protected 'primaryKey' => string 'id' (length=2) protected 'keyType' => string 'int' (length=3) public 'incrementing' => boolean true

######試したこと


var_dumpで$userを確認したところ、データが入っていないため
https://teratail.com/questions/250444?link=qa_related_pc
こちらの回答を参考にUsersController.phpに$user = User::find($id);
を追加しましたが結果は変わらず。
######バージョン


laravel 5.5.48
php 7.4.5
homestead

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

phper.k

2020/10/06 13:34 編集

参考にしたページなどがあれば、記載してください また、エラーメッセージは省略せず、全てを記載してください
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問