Let's say I have Vehicle model (it's Eloquent model) that stores different types of vehicles (in vehicles table). Of course, there are many different types of vehicles, so I have for example:
class Car extends Vehicle {
}
class Bicycle extends Vehicle {
}
and so on.
now I need to find object based on vehicle and here's the problem. I've added the following method in Vehicle model:
public function getClass()
{
return __NAMESPACE__ . '\\' . ucfirst($this->type)
}
so I can find the class name I should use.
But the only way to get valid class is like this:
$vehicle = Vehicle::findOrFail($vehicleId);
$vehicle = ($vehicle->getClass())::find($vehicleId);
which is not the best solution because I need to run 2 exact same queries to get valid final class.
Is there any way to achieve same without duplicating the query?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire