0
0

More than 3 years have passed since last update.

投稿機能を作る

Last updated at Posted at 2020-12-02

モデルの編集

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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0