【发布时间】:2016-06-07 09:56:05
【问题描述】:
我试图仅显示所选类别的产品,但出了点问题,我仍然无法理解。
到目前为止我所做的是我在routes.php中添加了路线
Route::get('/admin/category/single/{categoryId}', ['uses' => 'AdminController@categoriesCheck']);
然后在 AdminController 我添加了
public function categoriesCheck() {
$products = Product::paginate(15);
$categories = Categories::all();
return View::make('site.admin.single_category', [
'categories' => $categories,
'product' => $products
]);
}
所以 2 个问题如果我点击 category_id=1 如何进行查询以仅加载 category_id=1 的产品(我在产品表中创建了包含每个产品的 category_id 的列) 以及如何制作视图页面?
目前我有虚拟single_category.blade.php
@extends('layouts.master')
@section('title', 'Categories')
@section('content')
<div class="col-xs-12">
<h3>Products List</h3>
<hr />
<div class="row">
@foreach($products as $i => $product)
<div class="col-md-4">
<div class="panel panel-default text-center">
<div class="panel-heading">{{{ $product['title'] }}}</div>
<div class="panel-body min-h-230">
@if($product['image'])
<img class="max-150" src="{{ $product['image'] }}" alt="{{{ $product['title'] }}}" />
<br /><br />
@endif
{{ str_limit($product->description_small, 100) }}
@if (strlen($product->description_small) > 100)
<br/><br /> <a href="{{ URL::to('product/single/' . $product->product_id) }} " class="btn btn-info btn-block view-more">View More</a>
@endif
</div>
</div>
</div>
@if(($i+1) % 3 == 0)
</div><div class="row">
@endif
@endforeach
</div>
<hr />
{{ $products->links() }}
</div>
@endsection
当我现在运行页面时,我得到了所有产品......无论我点击什么类别
【问题讨论】:
-
您没有过滤您的产品,您只是对所有条记录进行分页。
-
是的,这就是我无法理解的......如何以及在哪里构建查询
-
您是否费心查看Laravel documentation?
-
@MartinBean 是的.. 但是我在整个框架和 mvc 模型中有点新,仍然有些东西有点令人困惑并且有点复杂.. 像这种情况