You could use the protected $table property to explicitly point your NewProduct class to the desired table. :)
PHP Classes and Inheritance
For reason I won't go into I'm facing an odd problem.
We have a Product class in a none Laravel PHP codebase which is called statically in lots of different places e.g. Product::get(), Product::all() etc.
Currently these queries contain vanilla SQL queries that get info from a "products" table. Our code runs on multiple websites using themes. I am facing an odd problem where theme 1 will get it's data from the current "products" table but theme2 will have to look in another table "new_products" in the same database. These tables will be structured differently and will link to other tables differently so I can't just map the data into the "products" table.
I'd like to not switch which table to use in the functions inside the "Product" class as that feels horrid. I'd also like to avoid switching classes in the calling function e.g.
if (theme1) $product = Product::get() else $product = NewProduct::get()
I wondered if there was a clean way to leave all the calls to the Product class as is and create a OldProduct and a NewProduct class which inherit Product. Product class could then have the if (theme1) else logic in? Bit of a mess but this is a temporary thing until all our sites are using the new table structure.
Another option is to fork the entire codebase but that will cause hosting overheads.
Any other options that are viable?
Any help greatly appreciated!
Why don't put something like
if (theme1) $productmodel = 'App\Product' else $productmodel = 'App\NewProduct' in a middleware ?
And then you call $productmodel::get().
I don't know the best place to store $productmodel (class, session, ...).
Please or to participate in this conversation.