Category: Laravel

When migrating a classic foreign key constrained relationship, such as belongsTo, over to a polymorphic relationship, you've got to be very sure you've covered all places that the previous foreign key was accessed or set. It is common that you will have several belongsTo relationships that all share the same foreign key name as well.

Now we have got to go and change our migrations, which if you mess up and don't refactor to be exactly the same as the original model based version is going to give you headaches.

To remedy this for our current application, we are going to create an accessor and a mutator to help catch any remaining references.

This accessor and mutator mean that we can still access and set the foreign key like we did before the migration, but it is going to report usages to our error tracker so we can jump in and fix any remaining references to the Image's blog_post_id attribute.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners