arctushar
3 months ago

nested eager eloquent not as expected

Posted 3 months ago by arctushar

My sector model is as below

class Sector extends Model
{
    public function codelists()
    {
        return $this->hasMany('App\Codelist');
    }

}

when I use below code

$sectors=Sector::with(['codelists'=>function($query){
            $query->select('id','code','sector_id')
            ->where('category','!=','z');
            }])->get();

and return $sectors, it is ok. Showing only id,code,sector.

but when using below code

foreach($sectors as $sectorkey=>$sectorvalues){
            foreach($sectorvalues->codelists as $codekey=>$codevalues){
                return $codevalues->get();
            }
        }

it is showing all columns of codelists But why ? what is solution if I need only selected three column ???

Please sign in or create an account to participate in this conversation.