Display sub categories on seperate page when clicked on category link in home page

Published 3 weeks ago by saadat_2003

I have displayed categories link on homepage and getting id in $category[cname] I have category id in sub_category table.now I want to display all sub categories belong to that category

RamjithAp

Go to your App/Categories.php model and write

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Categories extends Model
{
    /**
     * Get the comments for the blog post.
     */
    public function subcategories()
    {
        return $this->hasMany('App\SubCategories','foreign_key_column_name');
    }
}

Also in App/SubCategories.php model

public function categories()
{
    return $this->belongsTo('App\Categories','id');
}

Then in your controller

$category =  Category::find($id);
$subcategories= Category::find($id)->subcategories;

return view('index')->withCategory($category)->withSubcategories($subcategories); 

And In your view

@foreach($subcategories as subcategory)
     <span>{{ $subcategory->name}}</span>
@endforeach

For more details read https://laravel.com/docs/5.5/eloquent-relationships#one-to-many

saadat_2003

thanks

saadat_2003

@RamjithAp thanks for helping a lot.

I have done this all $category = Category::find($id); $subcategories= Category::find($id)->subcategories; Now getting error Undefined variable: id

satish9323

can you paste your controller method here ? I think you forgot to pass $id in controller's function.

saadat_2003

@satish9323

public function index(){

    $category =  Category::find($id);
    $subcategories= Category::find($id)->subcategories;
    
    return view('search-categories')->withCategory($category)->withSubcategories($subcategories); 
}
satish9323

what is your route ? Pass $id in function and then try again.

It should be like.

public function index($id){
     $category =  Category::find($id);
        $subcategories= Category::find($id)->subcategories;
    
    return view('search-        categories')->withCategory($category)->withSubcategories($subcategories); 
}
satish9323

@saadat_2003 did u try ?

saadat_2003

in categoies.blade.php

@foreach($categories as $category) {{$category['cname']}} @endforeach

in search-categories.blade.php @foreach($subcategories as $subcategory) {{ $subcategory->name}} @endforeach

in Category.php class Category extends Model { // public $table='categories'; protected $fillable=[

    'cid',
    'cname'
    
];
public function subcategories(){
    return $this->hasMany('App\SubCategory','cid');   
}

}

in SubCategory.php

class SubCategory extends Model { // public $table='categories_sub'; protected $fillable = ['cid', 'csname'];

        public function categories()
        {
            return $this->belongsTo('App\Categories','cid');
        }
    

}

in SubCatController

public function index($id) { //

    $category =  Category::find($id);
    $subcategories= Category::find($id)->subcategories;
    
    return view('search-categories')->withCategory($category)->withSubcategories($subcategories); 
}

in CategoriesController public function index() { // $categories = Category::all()->toArray(); return view('categories', compact('categories')); }

I just want when user click on category link herf in categories.blade.php , then It should go to search-categories.blade.php and display sub categories of that specific categories

in routes web.php

Route::get('/search-categories', function () { return view('search-categories'); }); Route::get('/categories', function () { return view('categories'); });

Route::resource('Categories','CategoriesController'); Route::resource('search-categories','SubCatController');

Route::get('/','[email protected]'); Route::get('/','[email protected]');

DB Tables Categories (cid,cname) Categories_sub(csid,cid,csname,) cid is category id but migration is not created and also not foreign key relationship

saadat_2003

@satish9323 I have commented whole scenerio . when I passed $id argument it gives this error Missing argument 1 for App\Http\Controllers\SubCatController::index()

satish9323

Your category link should be like

Main Category Name here $id is id of your category. and your modified routes for subcategory page will be like :

Route::get('/category/{id}','[email protected]');

saadat_2003

should I only change my route to the above anything else needed to change in all above code @satish9323

saadat_2003

@RamjithAp I have commented all my code above , kindly review it and tell me if their is anything wrong

saadat_2003

@RamjithAp Kindly help me ,How to write this mysql format query in laravel controller

SELECT cname FROM categories where cid IN(SELECT max(category) FROM items group by category order by category desc ) order by cid desc LIMIT 10

Please sign in or create an account to participate in this conversation.