Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

Laravel associative array in blade Url

This is my controller,there is a url issue Priviously i wass passing my data from Model but i face many issues. I want this type of url www.xyz.com/category/subcategory/subsubcategory. then i decide to pass data from controller data was successfully pass to blade , but i dont know how to show data in url.

    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'));

    }

Routes

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');

Blade

<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) }}">{{ AppCategory::find($category_id)->name }}</a></li>
                    @endif
                    @if(isset($subcategory_id))
                        <li ><a href="{{ route('products.category', AppSubCategory::find($subcategory_id)->category->id) }}">{{ AppSubCategory::find($subcategory_id)->category->name }}</a></li>
                        <li class="active"><a href="{{ route('products.subcategory', $subcategory_id) }}">{{ AppSubCategory::find($subcategory_id)->name }}</a></li>
                    @endif
                    @if(isset($subsubcategory_id))
                        <li ><a href="{{ route('products.category', AppSubSubCategory::find($subsubcategory_id)->subcategory->category->id) }}">{{ AppSubSubCategory::find($subsubcategory_id)->subcategory->category->name }}</a></li>
                        <li ><a href="{{ route('products.subcategory', AppSubsubCategory::find($subsubcategory_id)->subcategory->id) }}">{{ AppSubsubCategory::find($subsubcategory_id)->subcategory->name }}</a></li>
                        <li class="active"><a href="{{ route('products.subsubcategory', $subsubcategory_id) }}">{{ AppSubSubCategory::find($subsubcategory_id)->name }}</a></li>
                    @endif
                </ul>
            </div>
        </div>
    </div>
</div>

When i print on blade

<?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
        )

)

Kindly help me how to fetch this data in url

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To Access the current Route instance you are able to call Route::current();. Or, if you have a Request object by going $request->route().

To access all route parameters you can either call Route::current()->parameters() to get an associative array or you can access them as properties like so: Route::current()->categoryName.

(For further information, take a look the the offical documentaion https://laravel.com/docs/6.x/routing#accessing-the-current-route)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...