2022-02-27

Make Hidden child's relation /Laravel Eloquent

Model structure:

  • AccessoryGroup (hasMany: accessories)
  • Accessory (belongsTo: accessory_group)

Get all accessory groups with accessories (with accesory_group)

In accessories I needed accessory_group relation, to create some custom attribute (appends)

But after usage I don't won't my Api to return relation from accessories->accessory_group

AccessoryGroup
            ::with([
                'accessories' => function ($query) {
                    $accessory = $query->getRelated();
                    // Can I somehow use this $accessory relation for my problem
                    // something like $accessory->makeHidden('accessory_group'); - not working
                },
                'accessories.accessory_group',
            ])
            ->get();

when i add public $hidden = ['accesory_group']; in Accessory model I get what i want, but then it's always hidden (always need to use makeVisible)



No comments:

Post a Comment