*前提、実現したいこと
現在twitterのクローンアプリを作成していて、
userテーブル
・id
・name
tweetテーブル
・id
・user_id
・content
の2つのテーブルがあります。
・userは複数のツイートをすることができる
・userは他人のツイートに対して、いいねすることが出来る。
・tweetは複数のuserに対していいねされる。
実現したいことはuserのtweetを全て表示させることです。
*つまづいていること
viewで該当する箇所が何も表示されません。
*該当するソースコード
controller.php
php
1public function own(Request $request) 2 { 3 $tweets = Tweet::where('user_id', 'Auth::user()->id')->get(); 4 5 return view('admin.tweet.mypage', ['tweets' => $tweets]); 6 }
blade.php
php
1<tr> 2 <th>tweet一覧</th> 3 @if( $tweets != NULL) 4 @foreach( $tweets as $tweet) 5 <td><a href={{ action('Admin\TweetController@detail', ['id' => $tweet->id]) }}>{{ $tweet->content }}</a></td> 6 @endforeach 7 @else 8 <h5>ツイートはありません</h5> 9 @endif 10 </tr>
user.php
php
1public function tweets() 2 { 3 return $this->belongsToMany('App\Tweet'); 4 }
tweet.php
php
1public function users() 2 { 3 return $this->belongsToMany('App\User'); 4 }
dd($tweets)をしたところ
php
1Collection {#227 ▼ 2 #items: [] 3}
と表示されました。
以上です。
よろしくお願いします。