I have a relation between Categories and Expenses. An Expense belongsTo a Category, and it's linked using a foreign key in the database. Whenever I delete a category, all linked expenses will be deleted as well, as expected.
Now, I also have a relation between an Expense, and many Transactions, using a One-to-Many polymorphic relation. Using the method below, when I delete an Expense, all Transactions will be deleted with it, as expected.
But, when I delete a Category, all Expenses will be deleted as well as expected, but not the Transactions from either Expense. Why is this and how can I make sure all Transactions are deleted as well for all Expenses, when I delete a Category?
Deleting event in Expense model:
protected static function boot() {
parent::boot();
static::deleting(function($expense) {
$expense->transactions()->delete();
});
}
Relation Expense -> Transactions:
public function transactions()
{
return $this->morphMany(Transaction::class);
}