LoginSignup
2
0

More than 1 year has passed since last update.

【Laravel 8】ユーザー真偽をして表示/非表示

Last updated at Posted at 2022-01-31

はじめに

  • Laravel 初学者の私が学習した内容をアウトプットしていく記事になります。 冗長している点や軽い書き方などあれば御指南をお願いいたします。。。📝

ユーザー真偽をして表示/非表示

  • 実装する内容 今回は、ユーザー自身のポートフォリオをBootstrap tabを使用して、ユーザー自身のマイページならタブを表示 / 他ユーザーのページに行ったらタブを消す仕様です。

詰まったこと

  • 条件をどう分岐させるか的を得てない。 今回の解決策はこれ!URL の id と ログインユーザーが一緒なら ture URL に注目していなかった。根本的にロジックが立てられていない状態なので、そこに着眼点を置くことができていませんでした。
if (Auth::id() === $user->id)

「訪れているページが自分か他人か」 このロジックが今回は正解でした。
今思えば、なに詰まることが事があるんだと恥ずかしくなります。
該当コードが以下のものになります。

UserController.php
public function show(User $user, Request $request)
    {
        $user->load('portfolios.technologies', 'portfolios.user');
        $introduction = ReplaceClickableUrl($user->user_self_introduction);
        $currentUser = Auth::user();
        $isPublished = $request->input('is_published');
        $isPublished = (bool)$isPublished;
        $portfolios = Portfolio::where('is_published', $isPublished)->orderBy('created_at', 'desc')->where('user_id', $user->id)->get();
        $portfolios->load('portfolioLikes');

        if (Auth::guard('company')->check()) {
            return view('user.profiles.show', compact('user', 'portfolios', 'introduction', 'currentUser'));
        }

        return view('user.profiles.show', compact('user', 'portfolios', 'introduction', 'currentUser','isPublished'));
    }

最初は、$portfolios に ->get() していたので first() に変更してとか試してました。しかし、下の条件分岐で count メソッドがあるので、複数ある場合はエラーを吐かれるそう。
今回は、 controller 側で完結だったのでだいぶ蛇足を踏みました。。。

show.blade.php
<div class="row justify-content-center mt-5">
        <div class="col-md-10">
            @if (Auth::id() === $user->id)
            <nav>
                <div class="nav nav-tabs" id="nav-tab" role="tablist">
                    <a class="nav-item nav-link inline-block text-gray-500 hover:text-gray-600 hover:border-gray-300 rounded-t-lg px-4 text-sm font-medium text-center border-transparent border-b-2 dark:text-gray-400 dark:hover:text-gray-300 {{ $isPublished ? 'active' : '' }}" id="nav-published-tab" data-tab-item="published" href="?is_published=1"> 公開 </a>
                    <a class="nav-item nav-link inline-block text-gray-500 hover:text-gray-600 hover:border-gray-300 rounded-t-lg px-4 text-sm font-medium text-center border-transparent border-b-2 dark:text-gray-400 dark:hover:text-gray-300 {{ !$isPublished ? 'active' : '' }}" id="nav-draft-tab" data-tab-item="draft" href="?is_published=0"> 下書き </a>
                </div>
            </nav>
            @endif
            @include('components.mypageTab')
        </div>
    </div>
mypageTab.php
div class="tab-content" id="nav-tabContent">
    <div class="tab-pane fade show active" id="nav-published" role="tabpanel" aria-labelledby="nav-published-tab">
        <div class="row text-center pt-3">
            @if (count($portfolios) !== 0)
                @foreach ($portfolios as $portfolio)
                    @include('components.portfolio-card')
                @endforeach
            @else
                <div class="col-12 mb-3 mt-5">
                    <p>現在まだポートフォリオの投稿はありません</p>
                </div>
            @endif
        </div>
    </div>
</div>

こんなことで躓くのか。。ハぁ😔と悔しかったです。しかし、これからもいろんな事象を解決できるようにインプットをしていきたいと思います。
これからも、アウトプットを続けていきます!!
最後まで、読んでいてだきありがとうございました🙇🏻‍♂️

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