如何将一个类别重定向到子类别?

How to redirect one category to sub category?

这是 FrontController

public function index(){
    $categories = DB::table('categories')
        ->select('category')
        ->groupBy('category')
        ->get();
    return view('front', compact('categories'));
}

这是blade布局

@foreach($categories as $category)
<a href=""><h2 class="card-title">{{$category->category}}</br></h2></a>
@endforeach

我的问题是当我点击任何类别重定向到与子类别相关的...我该怎么做?

在您的控制器中,您可以执行以下操作:

// for categories
public function index() {
    $categories = DB::table('categories')
        ->select('category')
        ->groupBy('category')
        ->get();
    return view('front', compact('categories'));
}

// for sub categories
public function subCategory($category) {
    $sub_categories = DB::table('categories')
        ->where('category', $category)
        ->get();
    return view('another_front', compact('sub_categories'));
}

在你的blade中:

// for category
@foreach($categories as $category)
    <a href="{{ url('/subcategory') }}/{{$category->category}}"><h2 class="card-title">{{$category->category}}</br></h2></a>
@endforeach

// for sub category
@foreach($sub_categories as $subcategory)
        <a href="#"><h2 class="card-title">{{$subcategory->sub_category}}</br></h2></a>
    @endforeach

路线是:

Route::get('/subcategory/{category}', 'FrontController@subCategory');

你能试试这个吗

这是路由文件"web.php"

project_url /category/{catid}

这是blade布局

@foreach($categories as $category) <a href="{{project url / category/$category->id}}"><h2 class="card-title">{{$category->category}}</br></h2></a> @endforeach