简体   繁体   English

在laravel 5.4中按用户计数记录

[英]Count records by users in laravel 5.4

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. 我有与users表相关的loan_applications表。

In my view I count this way {!! $ loanapplications-> count () !!} 在我看来,我是这样计算的{!! $ loanapplications-> count () !!} {!! $ loanapplications-> count () !!} and tell me all the records. {!! $ loanapplications-> count () !!}然后告诉我所有记录。

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. 但是我需要按用户计数记录,我尝试使用Auth :: user ()方法,但目前无法执行。

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: 这是我的控制器中的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'));
}

And this is the counter in the element a href 这是元素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? 有人可以给我个小方向吗?

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 : 因此,在您的show方法中,您可以执行以下操作:

$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. 然后将$user返回到您的视图,您将在该对象上获得属性loan_applications_count

*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. *这不是基于Authenticated User的,它是为了使您能够动态地将用户作为参数传递,如果您想对Authenticated user进行操作,则仍然可以。

**Of course you have to define the loan_applications relationship in your User model. **当然,您必须在User模型中定义loan_applications关系。

[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. 您可以通过调用Auth::user()->id检索经过身份验证的用户的Auth::user()->id ,然后将其传递给find方法,或者使用Auth::user()->loan_applications->count访问关系,但是我不确定这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM