You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a ManyToMany relationship between Upsell Pages and Products. The intermediate (morph) table is "upsell_products".
class UpsellPage extends Model {
...
publicfunctionproducts(): MorphToMany
{
return$this->morphToMany(product::class, name: "upsell_configurable", table: "upsell_products")
->using(UpsellProduct::class)
->wherePivotNull("deleted_at")
->withPivot(["options", "sort_key"]);
}
}
My UpsellProduct MorphPivot looks like this, it is very basic. Of note is the "use SoftDeletes" line:
class UpsellProduct extends MorphPivot
{
use SoftDeletes;
protected$table = "upsell_products";
protected$fillable = ["sort_key", "product_id"];
publicfunctionupsell_configurable(): MorphTo
{
return$this->morphTo();
}
}
Normally, I'd expect Laravel to ignore models that are soft deleted. For example, if I had just a regular HasMany relationship with products, retrieving a list of products like so
// the query has where deleted_at = null$upsellPage = UpsellPage::find($upsellPageId)->products;
But in the case of the ManyToMany with a pivot, this is not the case. I have to explicitly add the ->wherePivotNul("deleted_at") in the MorphToMany relationship.
Is this an intentional design decision? If so, can I read about why this decision was made?
And secondly, is there a reason why there is no "wherePivotTrashed" and it's sibling method "wherePivotNotThrashed" methods for pivot relationships? I find it strange that I have to use ->wherePivotNull('deleted_at'), when I'd expect ->wherePivotNotThrashed
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a ManyToMany relationship between Upsell Pages and Products. The intermediate (morph) table is "upsell_products".
My UpsellProduct MorphPivot looks like this, it is very basic. Of note is the "use SoftDeletes" line:
Normally, I'd expect Laravel to ignore models that are soft deleted. For example, if I had just a regular HasMany relationship with products, retrieving a list of products like so
But in the case of the ManyToMany with a pivot, this is not the case. I have to explicitly add the
->wherePivotNul("deleted_at")
in the MorphToMany relationship.Is this an intentional design decision? If so, can I read about why this decision was made?
And secondly, is there a reason why there is no "wherePivotTrashed" and it's sibling method "wherePivotNotThrashed" methods for pivot relationships? I find it strange that I have to use
->wherePivotNull('deleted_at')
, when I'd expect->wherePivotNotThrashed
Beta Was this translation helpful? Give feedback.
All reactions