Tarasovych
8 months ago
15
18
Laravel

What is the right way of reusing model's methods?

Posted 8 months ago by Tarasovych

Hello!

So, I have a model:

public function doSomeDBStuff() {...}
public function doSomeElseDBStuff() {...}

and I want to reuse these methods.

What's the best way?

private $myModel;

public function __construct()
{
    $this->myModel = new MyModel();
}

public function method1()
{
    $this->myModel->doSomeDBStuff();
}

public function method2()
{
    $this->myModel->doSomeElseDBStuff();
}

or

public function method1()
{
    $myModel = new MyModel();
    $myModel->doSomeDBStuff();
}

public function method2()
{
    $myModel = new MyModel();
    $myModel->doSomeElseDBStuff();
}

?

Thanks in advance!

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