samedi 23 septembre 2017

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.

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:

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();
    });
}

This is my model:

class LoanApplication extends Model
{
    protected $table = 'loan_applications';

    protected $fillable = ['order', 'loan_quantity', 'loan_interests', 'loan_type', 'loan_time', 'status', 'client_id', 'user_id', 'comments' ];

    public function scopeOrder($query, $name)
    {
        $query->Join('clients','loan_applications.client_id','=','clients.id')
              ->select(
                'loan_applications.*',
                'clients.name as client_name', 
                'clients.last_name as client_lname',
                'clients.phone_number as client_phone')
              ->where('loan_applications.order','like',"%$name%")
              ->orWhere('loan_applications.loan_type','like',"%name")
              ->orWhere('loan_applications.client_id','like',"%$name%")
              ->orWhere('loan_applications.status','like',"%$name%");
    }

    // Relation with Client
    public function client()
    {
      return $this->belongsTo('App\Client', 'client_id');
    }

    // Relation with User
    public function user()
    {
      return $this->belongsTo('App\User', 'user_id');
    }
}

This is my show method in my controller:

public function show($id)
{    
    $loanapplications = DB::table('loan_applications')->get();

    return view('loanapplications.show',compact(loanapplications'));
}

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?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2yk6M9r
via IFTTT

Aucun commentaire:

Enregistrer un commentaire