Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5:按类别对产品进行分类,_Php_Laravel_Model_Scope_Eloquent - Fatal编程技术网

Php Laravel 5:按类别对产品进行分类,

Php Laravel 5:按类别对产品进行分类,,php,laravel,model,scope,eloquent,Php,Laravel,Model,Scope,Eloquent,我对laravel没有太多的经验,但我正在尝试对我的产品进行分类。。。 我已经可以对所有产品进行分类,但不能分类 你们能帮帮我吗 型号: class Product extends Model { protected $table = 'products'; public function getCategoriesListAttribute(){ return $this->categories()->lists('id'); } public function

我对laravel没有太多的经验,但我正在尝试对我的产品进行分类。。。 我已经可以对所有产品进行分类,但不能分类

你们能帮帮我吗

型号:

class Product extends Model {


protected $table = 'products';


public function getCategoriesListAttribute(){
    return $this->categories()->lists('id');
}


public function categories(){
    return $this->belongsToMany('App\Modules\Webshop\Models\ProductCategory', 'category_product', 'product_id', 'category_id')->withTimestamps();
}}
产品表:

    Schema::create('products', function(Blueprint $table)
    Schema::create('category_product', function(Blueprint $table)
    Schema::create('product_categories', function(Blueprint $table)
    Schema::create('product_tag', function(Blueprint $table)
控制器:

class ProductsController extends Controller {

    public function __construct()
    {
        //$this->middleware('auth');
    }

    public function index()
    {
        $viewmodel = array(
            "categories"=> ProductCategory::where('visible', '1')->get(),
            "products" => Product::has('categories')->get(),
            "page"=>Page::where('href','=', '/')->first(),
        );
        return view('Webshop::frontend.view.products.index', $viewmodel);
    }

}
index()仅显示具有任何类别的产品

如果你们需要更多信息,请随时询问:)

编辑:

我正在我的控制器中尝试

 public function index()
{
    $viewmodel = array(
        "products" => Product::with('categories')->where('category_id','23'),
    );
    return view('Webshop::frontend.view.products.index', $viewmodel);
}
这是:

  $viewmodel = array(
        "products" => Product::leftJoin('category_product', 'category_product.product_id', '=', 'products.id')
                        ->leftJoin('product_categories', 'product_categories.id', '=', 'category_product.category_id')
                        ->where('category_id', 23)
                        ->first(['products.*']),
    );
    return view('Webshop::frontend.view.products.index', $viewmodel);

我有一个id为23的类别。

好吧,如果你真的是指“排序”,请使用
orderBy()
querybuilder方法。如果需要按位于另一个表中的某些数据排序,请使用
join()
将这些字段包括到查询中。请注意,在Eloquent Builder中使用
join()
时,还需要使用
select(“products.*)
,否则Laravel将尝试在查询结果中提取不正确的列

如果需要按类别对查询结果进行分组,请使用查询结果集合的
group()
方法。您可以按
category\u id
字段分组。

修复了它

我把这个添加到我的模型中

 public function scopeGetCategorieSlug($query, $category_slug)
{
    return $query->leftJoin('category_product', 'category_product.product_id', '=', 'products.id')
                ->leftJoin('product_categories', 'product_categories.id', '=', 'category_product.category_id')
                ->where('product_categories.slug', $category_slug)
                ->get(['products.*']);
}
这是我的控制器

public function show($category_slug)
{
    $viewmodel = array(
        "categories"=> ProductCategory::where('visible', '1')->get(),
        "products" => Product::getCategorieSlug($category_slug),
        "page"=>Page::where('href','=', '/')->first(),
    );
    return view('Webshop::frontend.view.products.index', $viewmodel);
}

到目前为止,你试过什么吗?是的,我花了很多时间试图找出如何展示某一类别的产品。。我已将控制器添加到我的问题中。我只是停留在控制器中的index()函数上。在这里,它显示了任何类别的所有产品。所以这不是具体的。什么是“按类别排序”您选择的产品具有特定的类别\u id您如何只能按一个类别排序?谢谢您的帮助!现在我正在执行左连接,但它返回以下错误:尝试获取非对象的属性。我做错了什么?你可以在我更新的帖子中看到代码。