-1

my base class is post and many submodel such as : video post , image post all class have specific attribute & inherit parent attrib & all class need specific behaviors

Problem

when find on post model elequent give super model(post) instance, its wrong i need instance of submodel

roox uoox
  • 81
  • 5
  • It is very unclear what you want to do. Please provide some more info and code examples. – Jerodev Nov 25 '15 at 16:00
  • And your models look like..? – benJ Nov 25 '15 at 16:00
  • I want inheritance sub model from model? – roox uoox Nov 25 '15 at 16:26
  • basically a model that shares attribute but had more details added on child model? this could be done several ways, simple eloquent (still uses **single main model**) or single table inheritance (uses **several main model** on one table). – Bagus Tesa Nov 25 '15 at 16:31

2 Answers2

0

If I understood you correctly, you need relationships

Add a hasMany relationship to your Post.php model:

public function videos()
    return $this->hasMany(App\PostVideo::class);
}

As long as your post_video table has a post_id column that references a post, you can call this relationship like this:

foreach($post->videos as $video) {
    // Do something
}

And the inverse relationship:

Add a relationship to your PostVideo.php model:

public function post() {
    return $this->belongsTo(App\Post::class);
}

And of course, if you have a video, you can access the post it belongs to by doing:

$video->post
Pistachio
  • 1,652
  • 1
  • 18
  • 38
0

It is looked like you want a single table inheritance. In laravel this could be done manually or use package like nanigans or intrip. To use single table inheritance manually, i could suggest you start with reading this stackoverflow question first. However, notice that single table inheritance put everything in a single table but refered by several models that have different behavior. If this is not what you want, just use simple eloquent queries and models - which already explained by Pistachio.

Community
  • 1
  • 1
Bagus Tesa
  • 1,317
  • 2
  • 19
  • 42