我正試圖顯示用戶在表關係中的記錄。用戶在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 –