Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

me10071990's avatar

I am able to add the value in both (cat and subcat) but on my website only menu showing how can make submenu of menu

I made a menu dynamically and trying to make sub-menu but getting nothing happend

here is Category model

public function subCategory() { return $this->hasMany('App\Category','sub_cat_id');

}

public function Category() { return $this->belongsTo('App\Category','sub_cat_id'); } class Subcategory extends Model { protected $fillable = ['id', 'sub_cat_id', 'sub_cat_name'];

public function category()

{ return $this->belongsTo('App\Category'); } } and route is

route::post('/Subcategory/store',[

'uses'=>"subCategoriesController@store",
'as'=>'Subcategories.store',

]);

route::get('/subcategory/edit/{id}',

[
    'uses'=>"subCategoriesController@edit",

    'as'=>'Subcategories.edit',
]);

route::post('/subcategory/update/{id}',

[
    'uses'=>"subCategoriesController@update",

    'as'=>'Subcategories.update',
]); 

And Controller for Subcateogry is public function store(Request $request) { $this->validate($request, [

    'sub_cat_name' =>'required',
    'sub_cat_id' => 'required'


]);

$data=[

    'sub_cat_id'=>$request->sub_cat_id,
    'sub_cat_name'=>$request->sub_cat_name,

];

$subcategory=Subcategory::create($data);
Session::flash('success', 'Subcategory has created successfully');

return redirect()->back();

} and Controller for Category

public function store(Request $request) {

$this->validate($request, [

    'name' =>'required'

]);

$category= new Category;

$category->name =$request->name;

$category->save();

Session::flash('success', 'Category has created successfully');
return redirect()->back();

}

and only I am able to add the value in both (cat and subcat) but on my website only menu showing how can make submenu of menu

0 likes
6 replies
devfrey's avatar

Could you please fix the formatting of your code? Half of it is outside code blocks.

me10071990's avatar

Controller(Sub category)

public function store(Request $request) { $this->validate($request, [

        'sub_cat_name' =>'required',
        'sub_cat_id' => 'required'


    ]);

    $data=[

        'sub_cat_id'=>$request->sub_cat_id,
        'sub_cat_name'=>$request->sub_cat_name,
   ];

    $subcategory=Subcategory::create($data);
    Session::flash('success', 'Subcategory has created successfully');

    return redirect()->back();

}

Controller(Category)

public function store(Request $request) {

    $this->validate($request, [

        'name' =>'required'

    ]);

    $category= new Category;

    $category->name =$request->name;

    $category->save();

    Session::flash('success', 'Category has created successfully');
    return redirect()->back();

}

Category Model

public function subCategory()
{
    return $this->hasMany('App\Category','sub_cat_id');

}

public function Category()
{
    return $this->belongsTo('App\Category','sub_cat_id');
}

subCategory model

class Subcategory extends Model { protected $fillable = ['id', 'sub_cat_id', 'sub_cat_name'];

public function category()

{
    return $this->belongsTo('App\Category');
}

}

me10071990's avatar

Category.php

public function Category()
{
    return $this->belongsTo('App\Category','sub_cat_id');
}

public function subCategory()
{
    return $this->hasMany('App\Category','sub_cat_id');

}

Please or to participate in this conversation.