问题
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) }}">{{ \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>
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
回答1:
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)
来源:https://stackoverflow.com/questions/59560680/laravel-associative-array-in-blade-url