2

I am fairly new to Laravel Eloquent way to handle models.

I need to create a base Product class with some common properties and relations and some other classes that have some other properties not in common.

I'll try to make a quick example: all products have a "serial_number", a "ship_date" etc...

But only the Bottle and the Bulb classes have a "glass_type" feature, while the Chair class have another one.

I am doing some

   abstract class Product extends Model {}
   class Bottle extends Product {}

but I think this way the Product doesn't have any attached db $table, and I don't know how to handle the migrations table and the logic behind.

What is the best method to apply here?

Thank you in advance

Carondimonio
  • 71
  • 1
  • 7
  • you can use traits instead of abstracting things like that, for the migrations just repeat the commun columns for simplicity, but if you have lot of common column look at Laravel polymorphic relations, look at this example : https://laravel.com/docs/5.4/eloquent-relationships#polymorphic-relations – yoeunes Aug 23 '17 at 15:25

2 Answers2

2

In your model (the child one that extends the base model) add the table name,
explictly for example:

class SomeChildModel extends BaseModel {

  // Manually set the table name
  protected $table = 'table_name';

}
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
Kuldeep Mishra
  • 3,846
  • 1
  • 21
  • 26
  • 1
    Just remembering that by doing it the `$table` property from the parent class `BaseModel` will also be altered. – Carlos Afonso Aug 23 '17 at 18:19
  • 1
    Hi Kuldeep, by doing so we specify the table of `SomeChildModel` (of course altering `BaseModel` itself. In my understandig we have to store all of the properties of `SomeChildModel` in the `$table` even the common ones (right?). I'd like a `base_model` table in which we do have all the ids and the common properties, then in the `SomeChildModel` table we reference the `BaseModel` id and store the additional properties. I think it's more logical this way, maybe I am wrong, I dont know – Carondimonio Aug 24 '17 at 08:57
  • You should remember of foreign keys if You want use eloquent relations of `BaseModel` when using `SomeChildModel` For example `variants` for `products`: class Product extends Model { public function variants() { return $this->hasMany('App\Variant', 'product_id'); } } And then You can use `bottle()->variants()` too. If foreign key will be not specified then will be bottle_id in query, which is incorrect. – Świeżu Dec 17 '19 at 16:15
-1

We have partially "solved" this by including all needed fields in the product model. In addition we have introduced a Type field ("glass","Chair") and created different getter methods.

Other than this you can use the more elegant method described here, an store each type in a separate table: Extending Eloquent Models in Laravel (use different tables)

Fabio
  • 1
  • 2
  • Thank you for your answer, how did you organize the Models logic? – Carondimonio Aug 24 '17 at 09:03
  • Sorry for the late answer. You can put the logic in the Model in the beginning. But I suggest using your own classes for the main part. Was this what you have been looking for? – Fabio Aug 30 '17 at 10:55