title: How to pass a user relationship in home view laravel
body: <p>I have 2 tables:</p>
  
  <p><strong>Users Table:</strong></p>
  
  <ul>
  <li>id</li>
  <li>first_name</li>
  <li>etc..</li>
  </ul>
  
  <p><strong>Transaction Table:</strong></p>
  
  <ul>
  <li>id</li>
  <li>user_id</li>
  <li>etc..</li>
  </ul>
  
  <p>I'm trying to run a check to see if the User id matches the user_id in the Transaction table.</p>
  
  <p>I need to do this from the home view.</p>
  
  <p>What I have:</p>
  
  <p><strong>User Model:</strong></p>
  
  <pre><code>public function transactions() {
      return $this-&gt;hasMany(Transaction::class);
  }
  </code></pre>
  
  <p><strong>Transaction Model:</strong></p>
  
  <pre><code>use App\User; ...
  
  public function user() {
      return $this-&gt;belongsTo(User::class);
  }
  </code></pre>
  
  <p><strong>Home Controller:</strong></p>
  
  <pre><code>public function index()
  {
      $currentuserid = Auth::user()-&gt;id;
      $transactions = DB::table('transactions')-&gt;WHERE($currentuserid , '=','user_id' );
  
     return view('home');
  
  }
  </code></pre>
  
  <p><strong>Home view:</strong></p>
  
  <pre><code>@if ($transactions == true)
   //Do stuff
  @else
   //Do other stuff
  @endif
  </code></pre>
  
  <p>The following keeps returning <code>true</code> even if the <code>Auth id</code> and <code>Transaction user_id</code> are mismatched.</p>
  
  <p>I also thought I could simply do this if statement based on the relationship, but no luck:</p>
  
  <pre><code>public function index()
  {
     return view('home');
  }
  -------------
  @if (Auth::user()-&gt;id == (Auth::user()-&gt;transactions-&gt;user_id))
  </code></pre>
  
  <p><strong>UPDATE:</strong> Using the below code errors if there is a mismatch with the <code>user_id</code> and 'auth id'. Does not return Mismatch in view. Does return Match if the <code>id</code> &amp; <code>user_id</code> match.</p>
  
  <pre><code>    public function index()
      {
          $currentUserId = Auth::user()-&gt;id;
          $transactions = Transaction::where('user_id','=',$currentUserId)-&gt;get();
          return view('home', compact('transactions'));
  
      }
  @foreach ($transactions as $transaction)
  
    @if($transaction-&gt;user_id == Auth::user()-&gt;id)
    Match
    @elseif ($transaction-&gt;user_id != Auth::user()-&gt;id)
    Mismatch
    @endif
  
  @endforeach  
  </code></pre>