モデルの編集
Artist.php
class Artist extends Model
public function followingArtist()
{
// このユーザがフォロー中のユーザのidを取得して、配列にする
$userIds = $this->followings()->pluck('users.id')->toArray();
// このユーザのidもその配列に追加
$userIds[] = $this->id;
// それらのユーザが所有するアーティストを絞り込み、返す。
return Artist::whereIn('user_id', $userIds);
}
コントローラーの編集
ArtistController.php
public function output()
{
/*-------------自分だけのタイムライン-------------------------------
//現在ログイン中のユーザIDを変数$user_idに格納する
$user_id = Auth::id();
//artistテーブルからuser_idカラムが変数$user_idと一致するレコード情報を取得し変数$artistsに格納する
//artistテーブルからuser_idカラムが変数$user_idと一致するレコード情報を取得し変数$artistsに格納する
$artists = Artist::whereUser_id($user_id)->get();
return view('welcome', [
'artists' => $artists
]);
-------------------------------------------------------------*/
$data = [];
if (\Auth::check()) {
// 認証済みユーザ(閲覧者)を取得
$user = \Auth::user();
// ユーザとフォロー中ユーザの投稿の一覧を作成日時の降順で取得
$followingArtist = $user->followingArtist()->orderBy('created_at', 'desc')->paginate(10);
$data = [
'user' => $user,
'follows' => $followingArtist,
];
}
// Welcomeビューでそれらを表示
return view('welcome', $data);
}
ビューに表示する
blade.php
@foreach ($follows as $follow)
<div class="artistList__row__items">
<a href="{{URL::to('artist/'.$follow->id)}}">
<div class="artistPanel">
<img src="{{ $follow->path }}" width="100%">
</div>
</a>
<p>{!! link_to_route('artist.show', $follow->name, ['id' => $follow->id]) !!}</p>
</div>
@endforeach