How can I select a model to use based on a variable?

Posted 1 year ago by JackJones

That's not a very good title, it's easy to explain but I'm not sure how to word it. I have 11 different types of Place, one is a macroregion:

abstract class MacroRegions extends Places
    protected $place_type = 'macroregion';

    public function update()
        $records = $this->getUpdateRecords();

        foreach($records as $title => $info)
            $info['type'] = $this->place_type;
            $info['name'] = $info['name'] ?? $title;
            $info['official_name'] = $info['official_name'] ?? $info['name'];

            MacroRegion::updateOrCreate(['title' => $title], ['title' => $title] + $info);

The update method is identical across all 11 classes, except for the Model.

How can I "pass" the model so I can move this method to the Places class?

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up


Reply to

Use Markdown with GitHub-flavored code blocks.