blade Url 中的 Laravel 关联数组

问题描述 投票:0回答:1

这是我的控制器,存在网址问题以前我从模型传递数据,但我面临很多问题。我想要这种类型的网址www.xyz.com/category/subcategory/subsubcategory。 然后我决定从控制器传递数据,数据已成功传递到刀片,但我不知道如何在网址中显示数据。

    public function search(Request $request, $category_name = null, $subcategory_name = null, $subsubcategory_name = null)
    {
        $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]);
        }
        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;
            }
        }
//call plz on whatsapp possible?
        $products = filter_products($products)->orderByRaw("RAND()")->paginate(12)->appends(request()->query());
//        echo $subcategory_id;
//        echo "<br>";
//        echo $category_id;
//die('Execution Breaks');
    $cat_detail = DB::table('categories')->where('id', $category_id)->first();
    $sub_cat_detail = DB::table('sub_categories')->where('id', $subcategory_id)->first();
    $sub_sub_cat_detail = DB::table('sub_sub_categories')->where('id', $subsubcategory_id)->first();
    $catArray = array(
        'cat_detail' => $cat_detail,
        'sub_cat_detail' => $sub_cat_detail,
        'sub_sub_cat_detail' => $sub_sub_cat_detail,
    );

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

    }

路线

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>

当我在刀片上打印时

<?php

    echo "<pre>";
    print_r($catArray);
    die();

?>

http://www..xyz.com/shoes/flats/sandals

Array
(
    [cat_detail] => stdClass Object
        (
            [id] => 1
            [name] => Shoes
            [banner] => uploads/categories/banner/category-banner.jpg
            [icon] => uploads/categories/icon/tkcsbQdl2kQJgCJi7EWsqDB4aq60YnmTYfxcy8pG.png
            [featured] => 1
            [top] => 1
            [slug] => Shoes
            [meta_title] => 
            [meta_description] => 
            [created_at] => 2019-12-23 11:54:02
            [updated_at] => 2019-10-16 21:53:52
        )

    [sub_cat_detail] => stdClass Object
        (
            [id] => 1
            [name] => Flats
            [category_id] => 1
            [created_at] => 2019-03-12 11:13:24
            [updated_at] => 2019-09-30 16:47:58
        )

    [sub_sub_cat_detail] => stdClass Object
        (
            [id] => 2
            [sub_category_id] => 1
            [name] => Sandals
            [brands] => ["3"]
            [created_at] => 2019-03-12 11:20:23
            [updated_at] => 2019-09-30 16:51:52
        )

)

请帮助我如何在 url 中获取此数据

php arrays laravel routes laravel-blade
1个回答
0
投票

要访问当前的

Route
实例,您可以调用
\Route::current();
。或者,如果您通过
Request
获得了
$request->route()
对象。

要访问所有路由参数,您可以调用

\Route::current()->parameters()
来获取关联数组,也可以将它们作为属性访问,如下所示:
\Route::current()->categoryName

(更多信息,请查看官方文档 https://laravel.com/docs/6.x/routing#accessing-the-current-route

© www.soinside.com 2019 - 2024. All rights reserved.