Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent
Last updated 2 years ago.
0

If my thoughts are correct. You want to include methods from another model into a seperate model...

In this case, you can use the __construct method in your model that is extending the external class:

   private $external;

    public function __construct(ExternalClass $external)
    {
        $this->external = $external;
    }

then, in other methods in the class that is extending the ExternalClass, you can call the method with:

    public function thingDoer() 
    {
        return $this->external->thingThatDoes();
    }

Code not tested so let me know if this helps!

Edit: I think in php5.4+ you may need to use:

class Model extends Eloquent {

    use External;

    public function thingDoer() 
    {
        return $this->thingThatDoes();
    }
}

class External {
     public function thingThatDoes(){ return $something };
}

There are a few ways to do this, but it depends on how your personal project is structured. Try to stay consistent with your practices.

Last updated 2 years ago.
0

class External { public function thingThatDoes(){ return $something }; }

You mean ... ;)

trait External { ... }
Last updated 2 years ago.
0

Traits. Exactly what I was looking for

Last updated 2 years ago.
0

WHOOPS! Forgive the slip up

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

iWader iwader Joined 14 Aug 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.

© 2024 Laravel.io - All rights reserved.