I have one problem. I try to create multiple models for one table in Laravel 5.6. I have a table for example Car, class for him:
class Car extends Model {
$fillable = ['type'];
public function test(){ }
}
So, the field type is required. E.g. model car can have multiple types (2-10..). Depending on the type I should run difference code in test function. Yes, i
can do multiple if
conctructions, but it's not true.
Based on this I would like to create more classes that will be extended from Car and will
to match to each of the types. For example:
class ElectricCar extends Car { $type = 'electric'; } ...
class SolarCar extends Car { $type = 'solar'; } ...
If I will create this structure than I get a few problems.
For example record for id = 1 is Electric car. What will I get if call Car::find(1)
Yes, I will get Car
class, but I need ElectricCar
class.
I saw https://github.com/Nanigans/single-table-inheritance, but it's not a good solution.
it violates one of the SOLID rules, where we haven't to change parent class when creating child class. So what is a solution we can give me based on SOLID
rules?