php - Laravel 4.1 Eloquent Relations with belongstoMany or morphToMany god know what -
programs has items , uitems table ( there can many programs , many programs can have same or different items whereas items can in different programs)
pivot table : has items_id , items_type items_id can have 'id' either items or uitems
so if list programs::find(1)->with('items', 'uitems') if items_type ===user use uitems else use items need this!
i have programs table:
id user_id name description 1 2 x workouti high intensity 2 1 yn workouti low intensity 3 2 c workouti massive workout 4 2 b oldm abi sfsdf
i have pivot table seen in picture below.
programs table has relation 2 tables below
- items "core" --> id | category_id | name | description
- uitems "user" --> id | user_id | name | description
if @ pivot table have 2 columns named items_id , items_type 2 represent items , uitems table so;
if(items_type === 'user') pull info uitems as; if(items_type === 'core') pull info items.
what have tried to;
route::get('ws/{id?}', function($id) { $workouts = array('monday'=>'', 'tuesday'=>'', 'wednesday'=>'', 'thursday'=>'', 'friday'=>'', 'saturday'=>'', 'sunday'=>''); $days = days::all()->lists("name", "id"); foreach($days $daykey => $day) { foreach($programs = programs::find($id)->items()->wherepivot('days_id', $daykey)->orderby('sorts_id', 'asc')->get() $prokey => $program) { $program->settings = settings::find($program->pivot->settings_id)->toarray(); if($program->pivot->items_type ==='core') { $program->items = items::find($program->pivot->items_id)->toarray(); }else if ($program->pivot->items_type ==='user') { $program->items = uitems::find($program->pivot->items_id)->toarray(); $program->by =user::find(uitems::find($program->pivot->items_id)->user_id)->toarray(); } $workouts[$day][] = $program->toarray(); } } $program = json_encode($workouts); return $program; });
but code results in 39 query attemps mysql not cool @ all. provides me want weak performance:
where says by:oguzhan piskin has info uitems whereas others items
i need accomplish using eloquent's eager loading. query builder find priority on eager loading eloquent. or clue highly appriciated!
programs model:
class programs extends eloquent
{ protected $table = "programs";
public function items() { return $this->belongstomany('items')->withpivot('settings_id', 'days_id', 'sorts_id', 'id', 'items_type', 'items_id'); }
since reach data using programs using items reach data when uitems data joins challange in pivot table cant use both.
try : read more here eager loading constrains
edit model :
public function items() { return $this->belongstomany('items')->wherepivot('items_type', 'core')->withpivot('settings_id', 'days_id', 'sorts_id', 'id', 'items_type', 'items_id') } public function uitems() { return $this->belongstomany('items')->wherepivot('items_type', 'users')->withpivot('settings_id', 'days_id', 'sorts_id', 'id', 'items_type', 'items_id') }
and in controller :
<?php route::get('ws/{id?}', function($id) { $workouts = array('monday'=>'', 'tuesday'=>'', 'wednesday'=>'', 'thursday'=>'', 'friday'=>'', 'saturday'=>'', 'sunday'=>''); $days = days::all()->lists("name", "id"); $program = programs::find($id)->with('items','items'); ?>
hope understood correctly wanted. please let me know
Comments
Post a Comment