Support the ongoing development of Laravel.io →
Database Eloquent IOC

HI, Ive got a system that Im building for a advertising company and they sell advertising a Medical Centres and Real Estates , So I created a Site model then then created Two Subclasses called MedicalSite and MovingSite. using Global scope classes.

So when I want a medical or moving (real estate) site I can just use the correct class but what I would like to do is have the correct site returned.

So my idea was to create a resolving binding and then in the callback I can check the site type and return the correct class so when I call Site::find(1) I should get a MedicalSite instance back as site id 1 is a medical Site.

This is just a first example of the function I came up with but It not working but It is being called

        $this->app->afterResolving('Apps4u\Sharples\Models\Site', function($site){
            if($site->id){
                if($site->site_type == 'medical') {
                    dd('med');
                    return new MedicalSite($site->toArray());
                }

                if($site->site_type == 'moving') {
                    dd('med');

                    return new MovingSite($site->toArray());
                }
            }
        });

Ive tried resolving and afterResolving both functions get called but the inner if statements dont fire.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

apps4u apps4u Joined 7 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.