Reputation: 565
I am trying to display the records that a user has in a table relation.
I have the loan_applications table that is related to the users table.
In my view I count this way {!! $ loanapplications-> count () !!}
and tell me all the records.
But I need to count the records by users, I tried using the Auth :: user ()
method, but at the moment I can not do it.
This is the relationship of the tables in the migration:
<?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->increments('id');
$table->string('order',16)->unique();
$table->string('loan_quantity');
$table->string('loan_interests');
$table->string('loan_type');
$table->string('loan_time');
$table->string('status');
$table->integer('client_id')->unsigned();
$table->foreign('client_id')
->references('id')
->on('clients')
->onDelete('cascade');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->string('comments')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('loan_applications');
}
}
This is my model:
<?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->get('criteria'))->orderBy('id','ASC')->paginate(8);
$loanapplications = DB::table('loan_applications')->get();
$pendings = DB::table('loan_applications')->where('status', '=', '0')->get();
$approved = DB::table('loan_applications')->where('status', '=', '1')->get();
$declined = DB::table('loan_applications')->where('status', '=', '2')->get();
//dd($data);
return view('loans.index',compact('data', 'loanapplications', 'pendings', 'approved', 'declined'))
->with('i', ($request->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')->find($id);
dd($data);
return view('loans.create',compact('data'));
}
}
This is my show method in my controller:
public function show($id)
{
$data = LoanApplication::find($id);
$clients = DB::table('clients')->find($id);
//$users = User::find($id);
$users = App\User::withCount('loan_applications')->find($id);
$loanapplications = DB::table('loan_applications')->get();
$pendings = DB::table('loan_applications')->where('status', '=', '0')->get();
$approved = DB::table('loan_applications')->where('status', '=', '1')->get();
$declined = DB::table('loan_applications')->where('status', '=', '2')->get();
//dd($data, $clients, $users);
return view('loanapplications.show',compact('data', 'clients', 'users', 'loanapplications', 'pendings', 'approved', 'declined'));
}
And this is the counter in the element a href
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-bell-o"></i>
<span class="label label-warning">{!! $loanapplications->count() !!}</span>
</a>
Someone who can give me a small orientation?
Upvotes: 0
Views: 1811
Reputation: 2101
From the official documentaiton :
$posts = App\Post::withCount('comments')->get();
foreach ($posts as $post) {
echo $post->comments_count;
}
So in your show
method, you can do something like :
$user = App\User::withCount('loan_applications')->find(X /* User ID */);
Then return the $user
to your view and you will get the property loan_applications_count
on that object.
*This is not based on the Authenticated User in order to allow you to dynamically pass the user as parameter, if you want to do it with the authenticated user, you still can.
**Of course you have to define the loan_applications
relationship in your User
model.
[Edit]
You can either retrieve the user ID of the authenticated user by calling Auth::user()->id
, then pass it to the find method, or access the relationship with Auth::user()->loan_applications->count
, but I'm not sure about this one.
Upvotes: 1