I'm working with an existing Database which has the following structure:
projects
-----------------
| id | name |
-----------------
| 1 | building |
| 2 | pavement |
-----------------
accounts
---------------
| id | name |
---------------
| 8 | income |
| 9 | oucome |
---------------
policies
----------------------------------
| id | project_id | canceled |
----------------------------------
| 14 | 1 | 1 |
| 15 | 1 | 0 |
| 16 | 2 | 0 |
----------------------------------
detailed_policies
---------------------------------------------------------------
| id | policy_id | account_id | project_id | amount | date
---------------------------------------------------------------
| 1 | 15 | 8 | 1 | 80.0 | 20151212
| 1 | 15 | 8 | 1 | 500.5 | 20160102
| 2 | 15 | 8 | 1 | 30000.0 | 20160102
| 2 | 16 | 9 | 2 | 9500.0 | 20160622
---------------------------------------------------------------
- A project has many policies, detailed_policies.
- A policy has many detailed_policies.
- An account has many detailed_policies.
So far the models look like this:
class Project extends \Eloquent
{
public function policies()
{
return $this->hasMany('Models\Policies');
}
public function detailed_policies()
{
return $this->hasMany('Models\DetailedPolicies');
}
}
class Account extends \Eloquent
{
public function detailed_policies()
{
return $this->hasMany('Models\DetailedPolicies');
}
}
class Policies extends \Eloquent
{
public function project()
{
return $this->belongsTo('Models\Project');
}
public function detailed_policies()
{
return $this->hasMany('Models\DetailedPolicies');
}
}
class DetailedPolicies extends \Eloquent
{
public function project()
{
return $this->belongsTo('Models\Project');
}
public function account()
{
return $this->belongsTo('Models\Account');
}
public function policies()
{
return $this->belongsTo('Models\Policies');
}
}
I'm new with eloquent and I'm trying to write a lazy-loaded eloquent query that will get me:
- Accounts affected. (level1)
- Projects involved. (level2)
- Policies & Detailed policies (level 3).
- Filtered by a range of date.
Query should return a nested JSON array. I'm thinking something like this:
{
"Projects" : [
{
"projects" : [
{
"project_id" : "1",
"name":"building site",
"accounts" : [
{
"account":"income",
"policies" : [
{
"policy_id" : "11",
"desc" : "..."
},
{
"policy_id" : "12",
"desc" : "..."
}
],
"detailed_policies" : [
{
"detailed_policy_id" : "1",
"amount" : "1800.00"
},
{
"detailed_policy_id" : "2",
"amount" : "500.00"
}
]
},
{
"account":"outcome",
"policies" : [
{
"policy_id" : "11",
"desc" : "..."
},
{
"policy_id" : "12",
"desc" : "..."
}
],
"detailed_policies" : [
{
"detailed_policy_id" : "1",
"amount" : "1800.00"
},
{
"detailed_policy_id" : "2",
"amount" : "500.00"
}
]
}
]
}, ...
I'm aware that the DB may need modifications such as a pivot table for Accounts and Projects.
Can anyone suggest an efficient way to approach this?
Note: Purpose of this is to consume the returned object in an AngularJS app that will display a summary by accounts or by projects.
Thanks in advance.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire