类别和子类别网址

时间:2019-12-24 23:31:36

标签: laravel parameters routing routes

您好,开发人员,             我想要类似www.xyz.com/category/subcateogry/subsubgcatogory的网址,它在类别中的工作,但是当我添加类别/子类别时,它显示错误500。解决方案。

这是我的家庭控制器

public function search(Request $request)
    {
        $query = $request->q;
        $brand_id = $request->brand_id;
        $sort_by = $request->sort_by;
        $category_id = (Category::where('name', $request->category_name)->first() != null) ? Category::where('name', $request->category_name)->first()->id : null;
        $subcategory_id = (SubCategory::where('name', $request->subcategory_name)->first() != null) ? SubCategory::where('name', $request->subcategory_name)->first()->id : null;
        $subsubcategory_id = (SubSubCategory::where('name', $request->subsubcategory_name)->first() != null) ? SubSubCategory::where('name', $request->subsubcategory_name)->first()->id : null;        $min_price = $request->min_price;
        $max_price = $request->max_price;
        $seller_id = $request->seller_id;

        $conditions = ['published' => 1];

        if($brand_id != null){
            $conditions = array_merge($conditions, ['brand_id' => $request->brand_id]);
        }
        if($category_id != null){
            $conditions = array_merge($conditions, ['category_id' => $category_id]);
        }
        if($subcategory_id != null){
            $conditions = array_merge($conditions, ['subcategory_id' => $subcategory_id]);
        }
        if($subsubcategory_id != null){
            $conditions = array_merge($conditions, ['subsubcategory_id' => $subsubcategory_id]);
        }
        if($seller_id != null){
            $conditions = array_merge($conditions, ['user_id' => Seller::findOrFail($seller_id)->user->id]);
        }

        $products = Product::where($conditions);

        if($min_price != null && $max_price != null){
            $products = $products->where('unit_price', '>=', $min_price)->where('unit_price', '<=', $max_price);
        }

        if($query != null){
            $searchController = new SearchController;
            $searchController->store($request);
            $products = $products->where('name', 'like', '%'.$query.'%');
        }

        if($sort_by != null){
            switch ($sort_by) {
                case '1':
                    $products->orderBy('created_at', 'desc');
                    break;
                case '2':
                    $products->orderBy('created_at', 'asc');
                    break;
                case '3':
                    $products->orderBy('unit_price', 'asc');
                    break;
                case '4':
                    $products->orderBy('unit_price', 'desc');
                    break;
                default:
                    // code...
                    break;
            }
        }

        $products = filter_products($products)->orderByRaw("RAND()")->paginate(12)->appends(request()->query());

        return view('frontend.product_listing', compact('products', 'query', 'category_id', 'subcategory_id', 'subsubcategory_id', 'brand_id', 'sort_by', 'seller_id','min_price', 'max_price'));
    }

它是我的路线。在类别中,它的工作正常,但是当我添加类别/子类别时,它显示错误500

Route::get('/{category_name}', 'HomeController@search')->name('products.category');
Route::get('/{category_name}/{subcategory_name}', 'HomeController@search')->name('products.subcategory');
Route::get('/{category_name}/{subcategory_name}/{subsubcategory_name}', 'HomeController@search')->name('products.subsubcategory');

我的观点

<div class="breadcrumb-area">
    <div class="container">
        <div class="row">
            <div class="col">
                <ul class="breadcrumb">
                    <li><a href="{{ route('home') }}">{{__('Home')}}</a></li>
                    <li><a href="{{ route('products') }}">{{__('All Categories')}}</a></li>
                    @if(isset($category_id))
                        <li class="active"><a href="{{ route('products.category', $category_id) }}">{{ \App\Category::find($category_id)->name }}</a></li>
                    @endif
                    @if(isset($subcategory_id))
                        <li ><a href="{{ route('products.category', \App\SubCategory::find($subcategory_id)->category->id) }}">{{ \App\SubCategory::find($subcategory_id)->category->name }}</a></li>
                        <li class="active"><a href="{{ route('products.subcategory', $subcategory_id) }}">{{ \App\SubCategory::find($subcategory_id)->name }}</a></li>
                    @endif
                    @if(isset($subsubcategory_id))
                        <li ><a href="{{ route('products.category', \App\SubSubCategory::find($subsubcategory_id)->subcategory->category->id) }}">{{ \App\SubSubCategory::find($subsubcategory_id)->subcategory->category->name }}</a></li>
                        <li ><a href="{{ route('products.subcategory', \App\SubsubCategory::find($subsubcategory_id)->subcategory->id) }}">{{ \App\SubsubCategory::find($subsubcategory_id)->subcategory->name }}</a></li>
                        <li class="active"><a href="{{ route('products.subsubcategory', $subsubcategory_id) }}">{{ \App\SubSubCategory::find($subsubcategory_id)->name }}</a></li>
                    @endif
                </ul>
            </div>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

因为在search声明中没有subcategory_name也没有subsubcategory_name,Laravel试图将这些值分配给具有该名称的方法参数,所以您必须将它们添加为参数:

public function search(Request $request, $category_name = null, $subcategory_name = null, $subsubcategory_name = null){...}

Laravel将为那些参数分配在您在路由中声明变量的部分中具有URL的值。

此外,作为提示,请看看route model binding