bipin's avatar
Level 2

how to create model for category subcategory and product

i have table category,subccategory and product & i want to achieve like this

1. mobile & accesory(category)
          -mobile(subcategory)
                    -iphone(product)

2.electronics(category)
          -laptops(subcategory)
                   -hp(product)

i want to send all table detail like categoryid,categoryname , subcategoryid, subcategoryname, productid, productname from controller to view so that i cant fetch all these detail in hierarchical way like above mention

i don't know how to create model and controller for these type of situation please help me guys if any video or refrence it will be great thanks in advance

0 likes
8 replies
Screenbeetle's avatar
Level 15

Morning bipin.

This is a bit rough but its something like this:

Category.php Model

public function products()
{
return $this->hasMany(Product::class);
}

public function subCategories()
{
return $this->hasMany(Category::class);
}

SubCategory.php Model

public function category()
{
return $this->belongsTo(Category::class);
}

Product.php Model

public function category()
{
return $this->belongsTo(Category::class);
}

Then in controller you get results:

$categories = Category::all();

Then in view:

@foreach($categories as $category)
        
    // echo out category title

    @foreach($category->subCategories as $subCategory)
    
        // echo out sub category title

            @foreach($subCategory->products as $product)

                // echo out product title

1 like
bipin's avatar
Level 2

@Screenbeetle its has only Category table value i want both product as well as subcategory

Screenbeetle's avatar

Hi @bipin

If you set eloquent relations between each table then you can access related records for products and sub categories through the category model.

@topvillas suggested package is a good idea but I would try and get your own code working first. Relations are a fundamental part of programming so you should understand it well before using packages IMHO

1 like
topvillas's avatar

I disagree, @Screenbeetle. I'm about to put a pizza in the oven. I've got no idea how the oven works but it cooks pizza very well.

Screenbeetle's avatar

Morning @topvillas

I agree actually. I just personally think in this instance the OP should probably get used to simple eloquent relations as part of his learning.

But yeah - after you know the essentials I totally agree with you. Why reinvent the wheel if someone has done a package that could save you a lot of working out.

bipin's avatar
Level 2

@Screenbeetle here is my controller

   public function myform(Request $request)
{
    $categories = Category1::all();
    dd($categories->subcategory->name);
  
    return view('myform1',compact('categories'));

}
model for category

class Category1 extends Model
{
protected $table = 'mastercategory';

protected $fillable = ['id','name'];


public function subcategories(){ 

    return $this->hasMany('App\Subcategory', 'id_mastercategory');
     }    
public function products()
{
return $this->hasMany('App\product1', 'id_subcategory');}
}

model for subcategory

 class Subcategory1 extends Model
 {
 protected $table = 'subcategory';
  protected $fillable = [
    'name',
    'id',
    'id_mastercategory'
   ];
public function category()
 {
  return $this->belongsTo('App\Category1');
 }  

model for product

 class product1 extends Model
 {
 protected $table = 'subling';

  protected $fillable = [
    'name',
    'id',
    'id_subcategory'
];
  public function category()
 {
return $this->belongsTo('App\Category1');

} } @Screenbeetle i m using you method as you describe above im using dd($categories->subcategory->name); its says that it doesn't collect subcategory eg:- Property [subcategory] does not exist on this collection instance

bipin's avatar
Level 2

@Screenbeetle it got my ans by modifing controller $categories = Category1::with(['subcategories','product'])->get(); thanks for you giving your valueable time

Please or to participate in this conversation.