1 year ago

Dynamic appends/hidden/visible on model relations.

Posted 1 year ago by websanova

I'm trying to find a way to dynamically set some model properties on a relation:


    'brands' => function ($q) {
        // Doesn't seem to affect the model in the builder.
             ->setVisible(['id', 'name']);

        // Show's correctly.

         // Chaining together also doesn't work...

         // Filter the relation and limit it's data
        $q->select('id', 'name')
              ->where('brands.status', 'active');

Basically I want to minimize the data getting sent back on relations, so many times for a sub list it just needs an id and name (sometimes otheres).

I'm aware that there are some methods to do this with specialized models and manual iteration using setVisible on the collection. However I want to have this on the relations themselves.

Not sure why the above same setVisible doesn't work on the model in the query builder. This would be ideal.

Is there anyway to achieve this?

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