[英]Display all products under sub-categories - Laravel 5.2
I need to display all the products under a certain sub-category.我需要显示某个子类别下的所有产品。 I already have a categories menu set up on the home page which shows all the parent and its sub-categories.
我已经在主页上设置了一个类别菜单,其中显示了所有父类别及其子类别。 when I click on the sub-categories, it takes me to the sub-categories page with its id listed in the url.
当我点击子类别时,它会将我带到子类别页面,其 id 列在 URL 中。 Problem Im having, is I dont know how to display all the products under that sub-category on that page?
我遇到的问题,是我不知道如何在该页面上显示该子类别下的所有产品吗?
Here is my route to display the sub-categories pages:这是我显示子类别页面的路线:
Route::group(['middleware' => ['web']], function () {
Route::get('/category/{id}', [
'uses' => 'PagesController@categoryDisplay',
'as' => 'category.show'
]);
}
here is my ProductController.php to display all products under a sub-category:这是我的 ProductController.php,用于显示子类别下的所有产品:
class PagesController extends Controller {
public function categoryDisplay($id) {
$products = Product::all();
//$cat_id = Product::where('cat_id', '=', 3);
return view('category.show', compact('products'));
}
}
Here is my page where im trying to display the products:这是我尝试展示产品的页面:
@extends('app')
@section('content')
<div class="container">
<h3 class="text-center">
Hi
@foreach($products as $product)
{{ $product->product_name }} <br>
@endforeach
</h3>
</div>
@endsection
My Category Model:我的类别模型:
class Category extends Model {
protected $table = 'categories';
protected $fillable = ['category'];
/**
* One sub category, belongs to a Main Category ( Or Parent Category ).
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function parent() {
return $this->belongsTo('App\Category', 'parent_id');
}
/**
* A Parent Category has many sub categories
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function children() {
return $this->hasMany('App\Category', 'parent_id');
}
/**
* One Category can have many Products.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function product() {
return $this->hasMany('App\Product', 'id');
}
}
My Products Model:我的产品型号:
class Product extends Model {
protected $table = 'products';
protected $fillable = [
'product_name',
'price',
'cat_id'
];
/**
* One Product can have one Category.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function category() {
return $this->hasOne('App\Category', 'id');
}
}
And this is how my products table looks like:这就是我的产品表的样子:
"cat_id" is the products sub-category “cat_id”是产品子类别
As you can see in my controller, if I pass in an actual number, like 3, it will show all the products under that cat_id, but obviously i need it to be dynamic.正如您在我的控制器中看到的,如果我传入一个实际数字,例如 3,它将显示该 cat_id 下的所有产品,但显然我需要它是动态的。
I got it: I had to change a few things like my Route:我明白了:我必须改变一些东西,比如我的路线:
Route::get('category/{id}','PagesController@displayProducts');
My Controller:我的控制器:
class PagesController extends Controller {
public function displayProducts($id) {
$categories = Category::where('id', '=', $id)->get();
$products = Product::where('cat_id','=', $id)->get();
return view('category.show', compact('products', 'categories'));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.