title: Count records by users in laravel 5.4
body: <p>I am trying to display the records that a user has in a table relation.</p>
  
  <p>I have the loan_applications table that is related to the users table.</p>
  
  <p>In my view I count this way <code>{!! $ loanapplications-&gt; count () !!}</code> and tell me all the records.</p>
  
  <p>But I need to count the records by users, I tried using the <code>Auth :: user ()</code> method, but at the moment I can not do it.</p>
  
  <p>This is the relationship of the tables in the migration:</p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Support\Facades\Schema;
  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Database\Migrations\Migration;
  
  class CreateLoanApplicationsTable extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('loan_applications', function (Blueprint $table) {
              $table-&gt;increments('id');
              $table-&gt;string('order',16)-&gt;unique();
              $table-&gt;string('loan_quantity');
              $table-&gt;string('loan_interests');
              $table-&gt;string('loan_type');
              $table-&gt;string('loan_time');
              $table-&gt;string('status');
              $table-&gt;integer('client_id')-&gt;unsigned();            
              $table-&gt;foreign('client_id')
                    -&gt;references('id')
                    -&gt;on('clients')
                    -&gt;onDelete('cascade');
              $table-&gt;integer('user_id')-&gt;unsigned();            
              $table-&gt;foreign('user_id')
                    -&gt;references('id')
                    -&gt;on('users')
                    -&gt;onDelete('cascade');
              $table-&gt;string('comments')-&gt;nullable();            
              $table-&gt;timestamps();
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::dropIfExists('loan_applications');
      }
  }
  </code></pre>
  
  <p>This is my model:</p>
  
  <pre><code>    &lt;?php
  
  namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use App\Http\Requests;
  use App\Http\Controllers\Controller;
  use App\LoanApplication;
  use App\Client;
  use App\Loans;
  use DB;
  
  class LoansController extends Controller
  {
      /**
       * Display a listing of the resource.
       *
       * @return \Illuminate\Http\Response
       */
      public function index(Request $request)
      {
          $data = Loans::order($request-&gt;get('criteria'))-&gt;orderBy('id','ASC')-&gt;paginate(8);
  
          $loanapplications = DB::table('loan_applications')-&gt;get();
  
          $pendings = DB::table('loan_applications')-&gt;where('status', '=', '0')-&gt;get();
  
          $approved = DB::table('loan_applications')-&gt;where('status', '=', '1')-&gt;get();
  
          $declined = DB::table('loan_applications')-&gt;where('status', '=', '2')-&gt;get();
  
          //dd($data);
  
          return view('loans.index',compact('data', 'loanapplications', 'pendings', 'approved', 'declined'))
              -&gt;with('i', ($request-&gt;input('page', 1) - 1) * 5);
      }
  
      /**
       * Show the form for creating a new resource.
       *
       * @return \Illuminate\Http\Response
       */
      public function create($id)
      {
          $data = DB::table('loan_applications')-&gt;find($id);
  
          dd($data);
  
          return view('loans.create',compact('data'));
      }
  }
  </code></pre>
  
  <p>This is my show method in my controller:</p>
  
  <pre><code>public function show($id)
  {       
  
      $data = LoanApplication::find($id);
  
      $clients = DB::table('clients')-&gt;find($id);
  
      //$users = User::find($id);
  
      $users = App\User::withCount('loan_applications')-&gt;find($id);
  
      $loanapplications = DB::table('loan_applications')-&gt;get();
  
      $pendings = DB::table('loan_applications')-&gt;where('status', '=', '0')-&gt;get();
  
      $approved = DB::table('loan_applications')-&gt;where('status', '=', '1')-&gt;get();
  
      $declined = DB::table('loan_applications')-&gt;where('status', '=', '2')-&gt;get();
  
      //dd($data, $clients, $users);
  
      return view('loanapplications.show',compact('data', 'clients', 'users', 'loanapplications', 'pendings', 'approved', 'declined'));
  }
  </code></pre>
  
  <p>And this is the counter in the element a href</p>
  
  <pre><code>&lt;a href="#" class="dropdown-toggle" data-toggle="dropdown"&gt;
      &lt;i class="fa fa-bell-o"&gt;&lt;/i&gt;
      &lt;span class="label label-warning"&gt;{!! $loanapplications-&gt;count() !!}&lt;/span&gt;
  &lt;/a&gt;
  </code></pre>
  
  <p>Someone who can give me a small orientation?</p>