Yes, it is possible to use dependency injection for Eloquent models in Laravel 9.0+. You can use the make method to inject the model into the constructor of the class. For example, you can inject the Car model into the Rental class like this:
class Rental
{
public $vehicle;
public function __construct(Car $vehicle){
$this->vehicle = $vehicle;
}
}
$rental = app()->make(Rental::class);
Then you can access the properties of the Car model like this:
$rental->vehicle->getEngine();
$rental->vehicle->getModel();
$rental->vehicle->getYear();
You can also use the make method to inject the Car model into the Vehicle class like this:
class Vehicle
{
public $type;
public function __construct(Car $type){
$this->type = $type;
}
}
$vehicle = app()->make(Vehicle::class);
Then you can access the properties of the Car model like this:
$vehicle->type->getEngine();
$vehicle->type->getModel();
$vehicle->type->getYear();
Hope this helps!