vendredi 20 juillet 2018

Laravel Eloquent OEM: get value from passing through multiple one to many relationships

I have three tables which is users, loans, statuses

The relationship is like this:

A user can have many loans. a loan has many status steps. in the statuses table I have a column called 'status', basically it telsl this step "yes", "no". "pending" sort of situation.

the table structure look like this:

users table
->id
->... 

loans table

->id
->... 
->user_id (it is the foreign key ->references('id')->on('users');

statuses table

->id
->...
->status  (can be "yes", "no", "pending")
->... 
->loan_id (it is the foreign key ->references('id')->on('loans');

the models look like this:

in the User model :

public function loans(){
    return $this->belongsToMany('App\Loan'); 
   }

in the Loan model:

 public function users(){
    return $this->belongsToMany('App\User'); 
   }  
  public function statuses(){
        return $this->hasMany('App\Status');
    }

in the Status model:

 public function loan(){
        return $this->belongsTo('App\Loan');
    }

My question is how to get the status "yes" number for each user. say I have five users, each user have multiple loans. each loan have, say 20 steps. but different loan many have differnt "yes" steps . I would like to use laravel eloquent ORM to get a array tell me each user get how many "yes" at certain time. So I would be able to loop through this array in my front end blade file to display users progress. thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire