3

I have a table named categories and here is it's structure:

id              bigint(20)      AUTO_INCREMENT  
name            varchar(255)    
description     varchar(255)    
short_name      varchar(255)    
picture         varchar(255)    
parent_category int(11)
category_type   tinyint(4)  

So each category has a category_type which can be one of these values:

1: Main Category

2: Superior Category

3: Secondary Category

4: Secondary Sub Category

Now I wanted to set a One To Many relationship between these categories.

For example: a main category has many superior categories and a superior category is related to one main category.

And this will apply to the rest of them as well.

But because I have only one Model which is Category, I don't know how to apply this relationship, so if you know, plz help me out...

Model Category.php:

class Category extends Model
{
    use HasFactory;
    protected $fillable = ['name','short_name','description','picture','category_type','parent_category'];

}
Pouya
  • 117
  • 1
  • 14
  • 1
    Sounds like you need a One To Many Polymorphic relationship - https://laravel.com/docs/8.x/eloquent-relationships#one-to-many-polymorphic-relations – Simon K Feb 05 '22 at 14:24

3 Answers3

3

You can write the following relations in the Category model:

public function parentCategory()
{
    return $this->belongsTo(Category::class, 'parent_category', 'id');
}

public function childCategories()
{
    return $this->hasMany(Category::class, 'parent_category', 'id');
}

You can later fetch the relations for example like this:

$cat = Category::where('category_type', 'main')->first();
$cat->childCategories; //all the categories where the main category has been set as parent category

Or

  $cat2 = Category::where('category_type', 'secondary')->first();
  $cat2->parentCategory; //the category object which has been marked as parent for that specific secondary category

If you want to filter per category type on the relations, you can do this as well:

public function superiorChildCategories()
{
    return $this->hasMany(Category::class,'parent_category','id')->where('category_type','superior');
}

To use:

$cat3 = Category::where('category_type', 'main')->first();
$cat3->superiorChildCategories; //all the superior type categories where the main category has been set as parent category, will return null if there is no superior type child categories

Don't forget to set the actual parent type relation on the database as well, so parent_category column should be the id of the parent category.

It is also tested, and works as expected.

Mátyás Grőger
  • 1,207
  • 2
  • 13
  • 24
0

As I know, Laravel relationships work between models and not within one model. I would recommend you to use separate models (and separate tables).

If all other categories are on the same level and they all belong to one main category, you can make one model for the main category and all others can have one model.

Ayenew Yihune
  • 1,059
  • 13
  • 19
-1
public function parent_category()
{
    return $this->hasMany(Category::class,'parent_category','id');
}
  • Can you add an example for showing how to call this method – Pouya Feb 05 '22 at 14:43
  • class PartCategory extends Model { use HasFactory; protected $fillable = ['PARENT','TITLE','IMAGE','DESCRIPTION']; //protected $with = ['parent_category']; public function parent_category() { return $this->belongsTo(PartCategory::class,'parent','id'); } public function parts() { return $this->hasMany(Part::class); } } – Mohammad Gitipasand Feb 05 '22 at 14:49