0

I am after some laravel advice on this. I have this situation (pseudo-code):

class Base extends Eloquent {}

class Child1 extends Base {}

class Child2 extends Base {}

They are models and all 3 have their own database tables. Essentially idea is that base model has attributes that are applicable to both children and each child has their own.

What I wanted to know is how can I access attributes from Base model from the child? For instance if the base model has attribute "title" I want to be able to do something like this:

In controller for example Child1::find(1)->title it would print out the title which is an attribute of Base. Currently it seems that if I do just the above, I cannot access attributes from Base without say declaring them first. Is there a laravel 4 specific way of achieving this? Or do I need to go though Base and specifically say: public $title = ''; public $some_other_attribute = '';

So that Child can access them directly? I have a FK from Child to base.

Thanks for the advice!

OwlPaste
  • 87
  • 1
  • 1
  • 7
  • `I cannot access attributes from Base without say declaring them first` - Of course not. How would you access properties that do not exist? Please clarify the question a bit more – thpl Jan 09 '15 at 14:01
  • Are you trying to access database attributes from Base from Child1? Or are you trying to set variables in base that will not change and then access them? – Laravelian Jan 09 '15 at 14:51
  • Sorry what I mean is if I do Base::find(1)->title; I can access it from the Base model, Laravel's Eloquent I imagine does something to allow this. Title is simply a column in a table. And what I want to do is Page::find(1)->title to also work So what I am asking for is is there something similar I can do in my setup where Laravel can take care of this loading for me? – OwlPaste Jan 09 '15 at 14:54
  • 2
    What you describe sounds more like [a polymorphic relationship](http://laravel.com/docs/4.2/eloquent#polymorphic-relations) to me. – Joseph Silber Jan 09 '15 at 14:55
  • 2
    Take a look at [this post about multi-table inheritance with Eloquent](http://stackoverflow.com/a/26704488/1903366) – lukasgeiter Jan 09 '15 at 15:04
  • Ah fantastic! that is exactly what I was after but I did not know what this was called! Many thanks! How do I mark this solved? xD – OwlPaste Jan 09 '15 at 15:26

0 Answers0