我正试图显示用户在表关系中的记录。用户在laravel中计数记录5.4
我有一个关系到用户表loan_applications表。
在我看来,我算这样{!! $ loanapplications-> count() !!}
,并告诉我的所有记录。
但我需要用户来算的记录,我尝试使用Auth :: user()
方法,但在现阶段,我不能这样做。
这是表中的迁移的关系:
<?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');
}
}
这是我的模型:
<?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'));
}
}
这是我的节目在我的控制器方法:
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'));
}
并且这是元素中的计数器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>
有人谁可以给我一个小的方向是什么?
非常感谢您的回答,但在理论上我尝试做的是用户,看到他已经产生了要求,这就是为什么做身份验证(验证用户::)计数的利益。 –
我刚编辑答案,检查是否有帮助:) –
返回与FatalErrorException调用成员函数count()null –