ruudvangeelen
6 days ago

Change $table var before executing static method

Posted 6 days ago by ruudvangeelen

Hi,

I have a "Orders" modal with a "orders_2019" table. Now in 2020 we have (ofcouse) the "orders_2020" table. The reason why is that we use external software; this software connects directly to MySQL to request all 2019 and/or 2020 records. Unfortunately the softwware is unable to filter on year.

In my Laravel app i also want to switch between 2019 and 2020. I've set the constructor of my Orders modal as follows:

public function __construct()
{
    $this->table = 'orders_2019'; // or 'orders_2020'
}

The moment i do "\App\Orders::all()" this will return 2019 or 2020 depending on the constructor.

BUT .. i have to set the constructor value manually. The question is; how can i automate this? I would be handy to pass a parameter to the constructor (?) or something like this:

\App\Orders::setTable('orders_2020');

$orders = \App\Orders::all();

and for 2019

\App\Orders::setTable('orders_2019'); \App\Orders::all();

Who can help me?

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