laravel / ideas

Issues board used for Laravel internals discussions.
939 stars 28 forks source link

Extending BelongsToMany with a AsHasManyRelation method #2594

Open mellester opened 3 years ago

mellester commented 3 years ago

I was iterating over all the relations added to a specific client Model and Nulling some sensitive attributes whilst preserving the relationship itself. And I had multiple BelongToMany relation's with custom Pivot Classes and attributes. That had to be dynamically accessed. So I was wondering if this is a nice feature to have. I used they below snippet of code to turn the Attached pivots into a HasMany relation object this only works because I have custom pivots classes. And are using the default ids.

if (is_a($pdo, BelongsToMany::class)) {
                $PivotName= $pdo->getPivotClass();
                if ($PivotName!= Pivot::class) {
                    $pdos[$key] = $pdo->getParent()->hasmany($PivotName);
                    dd($pdos[$key]->get());
                }
}

My request is as follows would it be possible to make a BelongsToMany method that does the same. Mayby Even when getPivotClass returns the default Pivot::class.

rimace commented 3 years ago

I'm not sure I understand this.

My request is as follows would it be possible to make a BelongsToMany method that does the same.

What does you code actually do? Do you mean a method that removes "sensitive data" from relationships? Is that the functionallity you suggest here?

mellester commented 3 years ago

Basically I would like to access a pivot model as if it is a hasMany relation @rimace Shomthing like $user->clients->getPivotRelation

Then I could pretend like the pivot table conecting clients and user is just another Model I can do model stuf with, Like protected $cast of some fields