php - Laravel Eloquent: Relationships multiple have issue & Eloquent “select” method not working with using “with” method -


i try made eloquent: relationships 3 model. please have on code.

    $account = account::select(['id', 'is_sign_contract', 'restaurant_name', 'state', 'phone_no', 'email', 'one_time_pick_up', 'sign_contract_date', 'created_at', 'oil_on_hand', 'binsize'])             ->with(['completedservice' => function($c) {                //$c->select('id');                 }])             ->with(['accountservice' => function($q) {                     $q->with(['serviceprovider' => function($qs) {                         $qs->select('id', 'company_name');                     }])->select('account_id', 'service_provider_id', 'service_id');                 }])                     ->whereraw($where)                     ->orderby('id', 'asc')->offset(54)->limit(1)->get(); 

if remove //$c->select('id'); select form above relation data if using display blank relationship block.

below image response in last image whole function there

enter image description here


enter image description here


enter image description here

in short without select works fine, if using select not working.

laravel loads relations after first query run. in order attach related model parent need select foreign key on related table laravel knows model attach child after query run.

the accountservice works because ytou selecting account_id on model attach account , serviceprovider works because selecting id on serviceprovider service_provider_id on accountservice after queries run laravel knows models attached where.

your query not working because not selecting account_id on child model.

the following work:

$account = account::select(['id', 'is_sign_contract', 'restaurant_name', 'state', 'phone_no', 'email', 'one_time_pick_up', 'sign_contract_date', 'created_at', 'oil_on_hand', 'binsize'])     ->with(['completedservice' => function($c) {         $c->select('id', 'account_id');     }])     ->with(['accountservice' => function($q) {         $q->with(['serviceprovider' => function($qs) {              $qs->select('id', 'company_name');         }])         ->select('account_id', 'service_provider_id', 'service_id');     }])     ->whereraw($where)     ->orderby('id', 'asc')->offset(54)->limit(1)->get(); 

Comments

Popular posts from this blog

service - Android MediaPlayer calls onCompletion before it already finished -

javascript - Training Neural Network to play flappy bird with genetic algorithm - Why can't it learn? -

javascript - Create a stacked percentage column -