laravel - Eloquent children relationship show parent and its children but not children as primary data in list -
i have following schema , relations built on eloquent model
customer
id parent_id name
1 null samsung electronic
2 null abc company
3 2 abc company 1
4 2 abc company 2
5 null big source
aging
id customer_id current
1 1 500
2 2 300
3 3 200
4 4 100
eloquent model **aging** public function customer(){ return $this->belongsto('app\customer,'customer_id','id'); } public function children(){ return $this->hasmanythrough('app\aging,'app\customer','parent_id','customer_id'); }
and resulted output query
return aging::with(['customer'=>function($q){ $q->select('id','name'); $q->where('parent_id',null); },'children'=>function($q){ $q->select('agings.id','customer_id','current'); $q->with(['customer'=>function($q){ $q->select('id','name'); }]); }])->select('id','customer_id','current')->paginate(5);
results
{ "total": 4, "per_page": 5, "current_page": 1, "last_page": 1, "next_page_url": null, "prev_page_url": null, "from": 1, "to": 4, "data": [ { "id": 1, "customer_id": 1, "current": 500, "customer": { "id": 1, "name": "samsung electronic" }, "children": [] }, { "id": 2, "customer_id": 2, "current": 300, "customer": { "id": 2, "name": "abc company" }, "children": [ { "id": 3, "customer_id": 3, "current": 200, "parent_id": 2, "customer": { "id": 3, "name": "abc company 1" } }, { "id": 4, "customer_id": 4, "current": 100, "parent_id": 2, "customer": { "id": 4, "name": "abc company 2" } } ] }, { "id": 3, "customer_id": 3, "current": 200, "customer": null, "children": [] }, { "id": 4, "customer_id": 4, "current": 100, "customer": null, "children": [] } ] }
if take @ last 2 data customer null because query performs select aging parent customer,
how parent aging , children
Comments
Post a Comment