Using contracts on models

  laravel, laravel-8, php

The IDE endlessly reminds us to use contracts when we type-hint function parameters with a model.

I gave in and tried to do the right thing by defining an interface + abstract class for the model.

I proceeded to add an interface and an abstract class that is extended by the model.


namespace AppModelsI18n;

use AppModelsProduct;
use AutogoContractsModelsProductInterface;

abstract class ProductBase extends Base implements ProductInterface
    public ?Store $store = null;
    public ?int $id;
    public ?int $images;
    public ?int $price;
    public ?Supplier $supplier;

It seems declaring properties typed with other models cannot by type-hinted, in the abstract class as I get the following error:

IlluminateFoundationBootstrapHandleExceptions::handleError("Trying to get property ‘id’ of non-object", "/Users/johnDoe/dev/store-api/tests/Unit/Mail/ClientMailTest.php", [Object(AppModelsStore)])

Is there a work-around for this?

Maybe interface and abstract class with defined properties need to be in place for those models as well?

Source: Laravel

Leave a Reply