0

I have two models in my Laravel project (in the 'app' folder) - Product and Seller.

In my Product.php I have (simplified):

class ProductMiniature{
  public $name;
  public $price;
}

class Product extends ProductMiniature{
  public $description;
  public $images;
  public $manufacturer;
}

Then my Seller.php (simplified):

class Seller{
  $name;
  $products = [];

  public function __construct(){
    array_push($this->products, new ProductMiniature();
  }

}

However, Laravel throws an error: Class 'App\ProductMiniature' not found

When I tried to push new Product() into $this->products, it works.

What can I do to use ProductMiniature() in other models? I don't want to make new models for each "variant" of this class.

David Sojka
  • 73
  • 1
  • 9
  • You should create a new model for ProductMiniature – thefabdev Dec 01 '19 at 14:16
  • @Abiola That's what I wanted to avoid... Milion models for each variant :-/ – David Sojka Dec 01 '19 at 14:23
  • Its not solid to define 2 classes in a single file. From what i see, you could use just the Product model or the Product model should extends the ProductMiniature – thefabdev Dec 01 '19 at 14:26
  • @Abiola Product() contains a lot of data and I would prefer to use it when the user is working with a particular product. When the user works with a "list of products" (hundreds or thousands), it would be better to give him just a "snippet" of it. But there are different variations of Product() and I didn't want to create a separated model for each of them. – David Sojka Dec 01 '19 at 14:31
  • 1
    Check out this answer https://stackoverflow.com/questions/29033625/is-it-possible-use-multiple-classes-under-the-same-namespace-in-the-same-file – George Myl Dec 01 '19 at 14:49
  • And is there a way how I can organize models? If I have to create multiple files for each class which is similar to others, then I would like to create a folder for them. But if I put my Product.php and ProductMiniature.php in "App/Products/", then I can't use them in other models... – David Sojka Dec 01 '19 at 17:35
  • 1
    I am guessing you are not familiar with how these files are autoloaded – lagbox Dec 01 '19 at 22:07

0 Answers0