Hello, I want to know how I can describe "is-a" relationships with Eloquent models. Example:
abstract class Item extends Eloquent {
...
}
class Cellphone extends Item {
...
}
class Laptop extends Item {
...
}
So, I could do Item::all()
to get all Items (Cellphones and Laptops). Also, Cellphones and Laptops should have different id's, so I could do Item::find($id)
.
Thanks in advance!
why not using jsut one model for cellphone and laptop and have a type_id
property to distinguish between those? i think you'd be off the best with that.
you could then even create a method on the model or using a respository to get cellphones or laptops only.(Item::whereTypeId(Item::$TYPES->CELLPHONE)->get()
)
Try looking int single-table-inheritance - basically you define an abstract parent class and extend it with some specific subclasses that can encapsulate their unique attributes.
http://laravel.io/forum/02-17-2014-eloquent-single-table-inheritance
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community