【问题标题】:Showing products only from selected category in Laravel仅显示 Laravel 中选定类别的产品
【发布时间】: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 模型中有点新,仍然有些东西有点令人困惑并且有点复杂.. 像这种情况

标签: php mysql laravel-4


【解决方案1】:

首先在您的 Category 模型中,您必须与您的 Products 建立关系,因此添加以下内容:

public function products()
{
    return $this->hasMany('Product');
}

然后在您的控制器中,您必须从路由中接受类别 ID 并通过它查询类别:

public function categoriesCheck( $categoryId ) 
{
    $category = Categories::with('products')->findOrFail( $categoryId );
    return View::make('site.admin.single_category', [
        'category' => $category
    ]);
}

最后在您的视图中检查一个类别是否有产品以及是否有循环:

@if($category->products->count())
    @foreach($category->products as  $product)
        //DIsplay product data
    @endforeach
@endif

【讨论】:

  • 感谢您的回答。我刚收到这个错误,看不到categories_id 来自哪里.. 应该是 category_id...Column not found: 1054 Unknown column 'products.categories_id' in 'where clause' (SQL: select * from products` where products.categories_id in (3))'`
  • 添加第二个参数,它是 Categories 模型 products() 方法 return $this-&gt;hasMany('Product', 'category_id'); 中的外部列名。
  • 所以,即使我在那里有protected $primaryKey = 'category_id';,它也不会自动采用 Ctaegories 模型中的 category_id..
  • 太好了,很高兴我能帮上忙 :)
猜你喜欢
  • 2020-06-10
  • 1970-01-01
  • 2018-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多