用户在laravel 5.4

时间:2017-09-23 15:11:31

标签: php mysql laravel laravel-5.3 laravel-5.4

我正在尝试显示用户在表关系中的记录。

我有与users表相关的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'));
    }
}

这是我控制器中的show方法:

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>

有人能给我一个小小的方向吗?

1 个答案:

答案 0 :(得分:1)

来自官方文件:

$posts = App\Post::withCount('comments')->get();

foreach ($posts as $post) {
    echo $post->comments_count;
}

因此,在show方法中,您可以执行以下操作:

$user = App\User::withCount('loan_applications')->find(X /* User ID */);

然后将$user返回到您的视图,您将在该对象上获得属性loan_applications_count

*这不是基于经过身份验证的用户,以便您可以动态地将用户作为参数传递,如果您想对经过身份验证的用户执行此操作,您仍然可以。

**当然,您必须在loan_applications模型中定义User关系。

[编辑]

您可以通过调用Auth::user()->id来检索经过身份验证的用户的用户ID,然后将其传递给find方法,或访问与Auth::user()->loan_applications->count的关系,但我不确定这个。