Laravel: Products,Categories and SubCategories! (Relation ships)

10,491

Solution 1

Here i may help u with this!

First , you can use only one table for categories and subcategory!

You can make this:

 |---------------------------------------
 |id| |parent_id|    |name|       |slug|
 |1 | | (NULL)  | Electronics | electronics
 |2 | | 1       | Phones      | phones

Now category electronic have children Phones

So in your Category.php model u can make that

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function parent() {
    return $this->belongsTo(self::class, 'parent_id');
}

/**
 * @return \Illuminate\Database\Eloquent\Relations\HasMany
 */
public function children() {
    return $this->hasMany(self::class, 'parent_id','id');
}

Now u can use foreach to display your children and category! Hope that helpful! ;)

Solution 2

It's classic problem related with databases You should use hierarchical child/parent relation. At first Your design of database is bad, just add one column with root category which has "0" in PARENT_ID.

CREATE TABLE CATEGORIES ( 
      CATEGORY_ID  NUMBER,
      PARENT_ID    NUMBER,
      NAME         VARCHAR(255), 
      CREATE_TS    TIMESTAMP(0));

and then read this : http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ & How to create a MySQL hierarchical recursive query & Bear in mind that You are mixing logic of GUI with SQL role. If You want to return in single query then Your presentation layer will be quite ugly ;-)

and also think about leaving just category_id in product table, if You have only 2 or 3 levels You don't need to point to subcategory and category.

Share:
10,491

Related videos on Youtube

Lubomir Stankov
Author by

Lubomir Stankov

Updated on June 04, 2022

Comments

  • Lubomir Stankov
    Lubomir Stankov almost 2 years

    I have a problem! I make something but, not work so i will ask u for help! So the problem is

    I have this tables

    products:
      name,subcategory_id,category_id,price,description
    Categories:
      name,slug,timestamps
    SubCategories:
      name,slug,timestamps
    

    I want to make when smo call this url /category/{category}/{subcategory} to get all products of subcategory are called! But when product don't have sub category to open only category , i mean /category/{category}

    Thanks guys!

    • Devon
      Devon almost 6 years
      So you're asking for us to help you write your controllers, routes, and models? Far too broad, give laravel.com/docs a go, they are very detailed.
    • Lubomir Stankov
      Lubomir Stankov almost 6 years
      No bro, i have Controller model and routes! I just want to help me with relationships
    • Devon
      Devon almost 6 years
      Then why are you asking about urls?
    • Lubomir Stankov
      Lubomir Stankov almost 6 years
      i just give examples!
    • Devon
      Devon almost 6 years
      Well, your first issue is you don't have any column relating subcategories to categories, but your question still seems to show you haven't made an effort reviewing the docs and attempting the relationships yourself.
  • Alejandro Beltran
    Alejandro Beltran over 4 years
    How retrieve Subcategory of subcategory. In this case I can get only one level of subcategory...
  • Jasbin Karki
    Jasbin Karki almost 4 years
    @AlejandroBeltran you can do something like this Category::whereNull('parent_id')->get();