LoginSignup
0
0

More than 1 year has passed since last update.

【Laravel】ログインしているユーザーを検索画面に表示させなくしたい

Posted at

なにをしているか

Laravel6.xでツイッタークローンサイト作成中。備忘録。

なにをしたいか

トップ画面から、サイトに登録しているユーザーの一覧を表示させたい。
ユーザーの検索機能を実装したが、ログインしているユーザー(自分)を表示させないようにしたい。
Controllerで検索機能実装済み。

コード

search.blade.php
@foreach($users as $user)
@if ($user->id !== Auth::user()->id)
<div class="userSearchList">
  <figure><img src="images/{{ $user->images }}"></figure>
  <p>{{ $user->username }}</p>
</div>
<div class="userSearchButton">
  @if (auth()->user()->isFollowing($user->id))
  <form action="/users/{{ $user->id }}/unfollow" method="post">
    @csrf
    <input type="submit" name="button" class="followButton" value="フォロー解除">
  </form>
  @else
  <form action="/users/{{ $user->id }}/follow" method="post">
    @csrf
    <input type="submit" name="follow" class="followButton" value="フォローする">
  </form>
  @endif
</div>
@endif
@endforeach

検索画面でユーザーのプロフィール画像・フォローボタンを設置しています(フォローしていなければフォローボタン、フォローしていればフォロー解除ボタンが表示される)

search.blade.php
@if ($user->id !== Auth::user()->id)

この部分でテーブルに登録されているユーザーとログインしているユーザーが不一致のユーザー(要するに自分以外)の表示が実装できました!
意外とシンプルに実装できました。

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