Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.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的股票单位进行分组来找到畅销书?_Php_Laravel_Laravel 5_Pdo_Eloquent - Fatal编程技术网

Php 我如何通过对Laravel的股票单位进行分组来找到畅销书?

Php 我如何通过对Laravel的股票单位进行分组来找到畅销书?,php,laravel,laravel-5,pdo,eloquent,Php,Laravel,Laravel 5,Pdo,Eloquent,我试图根据该产品上创建的库存单位数量来选择最畅销的商品。产品的库存单位越多=销售量越大 我的产品列表如下所示: Schema::create('product_list', function (Blueprint $table) { $table->increments('id'); $table->decimal('cost', 9, 2)->default(00.00); $table->integer('product_category')-

我试图根据该产品上创建的库存单位数量来选择最畅销的商品。产品的库存单位越多=销售量越大

我的产品列表如下所示:

Schema::create('product_list', function (Blueprint $table) {
    $table->increments('id');
    $table->decimal('cost', 9, 2)->default(00.00);
    $table->integer('product_category')->unsigned();            # IE Bespoke/Static/Dynamic
    $table->integer('product_type')->unsigned();                # IE Themes(Custom Or Not)/Number of pages
    $table->integer('product_score')->default(0);               # How many favourites does it have?
    $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
    $table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));

    $table->index(array('product_category', 'product_type'));

    $table->foreign('product_category')->references('id')->on('product_categories');
    $table->foreign('product_type')->references('id')->on('product_types')->onDelete('cascade');
    });
Schema::create('product_skus', function (Blueprint $table) {
    $table->increments('id');
    $table->string('reference');
    $table->integer('product_id')->unsigned();
    $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
    $table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));

    $table->index(array('product_id'));

    $table->foreign('product_id')->references('id')->on('product_list')->onDelete('cascade');
});
public function skus()
{
    return $this->hasMany(ProductSku::class);
}
我的products\u SKU表如下所示:

Schema::create('product_list', function (Blueprint $table) {
    $table->increments('id');
    $table->decimal('cost', 9, 2)->default(00.00);
    $table->integer('product_category')->unsigned();            # IE Bespoke/Static/Dynamic
    $table->integer('product_type')->unsigned();                # IE Themes(Custom Or Not)/Number of pages
    $table->integer('product_score')->default(0);               # How many favourites does it have?
    $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
    $table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));

    $table->index(array('product_category', 'product_type'));

    $table->foreign('product_category')->references('id')->on('product_categories');
    $table->foreign('product_type')->references('id')->on('product_types')->onDelete('cascade');
    });
Schema::create('product_skus', function (Blueprint $table) {
    $table->increments('id');
    $table->string('reference');
    $table->integer('product_id')->unsigned();
    $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
    $table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));

    $table->index(array('product_id'));

    $table->foreign('product_id')->references('id')->on('product_list')->onDelete('cascade');
});
public function skus()
{
    return $this->hasMany(ProductSku::class);
}
以下是我迄今为止使用“雄辩”所做的尝试——目前不太在意是否有其他人加入,只是让畅销书发挥作用:

$topSellers = DB::table('product_list')->join('product_skus', 'product_skus.product_id', '=', 'product_list.id')
                                       ->groupBy('product_skus.product_id')
                                       ->limit(5)
                                       ->get()
                                       ->all();
这给了我一个错误:

SQLSTATE[42000]:语法错误或访问冲突:1055“iezonsolutions.product_list.id”不在分组依据(42000)中

如何找到最畅销的商品?我在每件商品售出时都会为其创建一个SKU,这是跟踪畅销商品的一种方式,同时也为用户提供了独特的跟踪能力

更新以显示关系

product\u sku
有一个外键
product\u id
,它与
product\u列表
表列
id
相关。我想根据与产品id的关系对库存单位进行分组。大多数SKU被分组到单个产品id,我想从
产品列表
表中获取它们的产品

$topSellers = DB::table('product_skus')->join('product_list', 'product_skus.product_id', '=', 'product_list.id')
                                       ->orderBy('product_skus.product_id')
                                       ->limit(5)
                                       ->get()
                                       ->all();
现在正在分组正确的关系,但是,它给了我一个

array:3 [▼
  0 => {#725 ▼
    +"id": 2
    +"reference": "A400IEZON_"
    +"product_id": 2
    +"created_at": "2019-01-16 16:37:16"
    +"updated_at": "2019-01-16 16:37:16"
    +"cost": "100.00"
    +"product_category": 1
    +"product_type": 2
    +"product_score": 0
    +"rating": 0
    +"down_payment": "10.00"
  }
  1 => {#726 ▼
    +"id": 3
    +"reference": "C400IEZON_"
    +"product_id": 3
    +"created_at": "2019-01-16 16:37:25"
    +"updated_at": "2019-01-16 16:37:25"
    +"cost": "150.00"
    +"product_category": 1
    +"product_type": 3
    +"product_score": 0
    +"rating": 0
    +"down_payment": "10.00"
  }
  2 => {#727 ▼
    +"id": 3
    +"reference": "C400IEZON_"
    +"product_id": 3
    +"created_at": "2019-01-16 16:37:25"
    +"updated_at": "2019-01-16 16:37:25"
    +"cost": "150.00"
    +"product_category": 1
    +"product_type": 3
    +"product_score": 0
    +"rating": 0
    +"down_payment": "10.00"
  }
]

我想计算所有这些,所以在这里,id为
3的产品应该显示为顶部产品,后面是产品2。

您可以通过子查询实现这一点

$skus = ProductSku::selectRaw('COUNT(*)')
    ->whereColumn('product_id', 'product_list.id')
    ->getQuery();

$products = ProductList::select('*')
    ->selectSub($skus, 'skus_count')
    ->orderBy('skus_count', 'DESC')
    ->take(5)
    ->get();
如果
ProductList
ProductSku
有关系,这将容易得多。 然后你可以这样做:

例如,在
App\ProductList
中,可以有如下内容:

Schema::create('product_list', function (Blueprint $table) {
    $table->increments('id');
    $table->decimal('cost', 9, 2)->default(00.00);
    $table->integer('product_category')->unsigned();            # IE Bespoke/Static/Dynamic
    $table->integer('product_type')->unsigned();                # IE Themes(Custom Or Not)/Number of pages
    $table->integer('product_score')->default(0);               # How many favourites does it have?
    $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
    $table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));

    $table->index(array('product_category', 'product_type'));

    $table->foreign('product_category')->references('id')->on('product_categories');
    $table->foreign('product_type')->references('id')->on('product_types')->onDelete('cascade');
    });
Schema::create('product_skus', function (Blueprint $table) {
    $table->increments('id');
    $table->string('reference');
    $table->integer('product_id')->unsigned();
    $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
    $table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));

    $table->index(array('product_id'));

    $table->foreign('product_id')->references('id')->on('product_list')->onDelete('cascade');
});
public function skus()
{
    return $this->hasMany(ProductSku::class);
}
。。然后,您的查询将是:

ProductList::withCount('skus') 
    ->orderBy('skus_count', 'DESC')
    ->take(5)
    ->get();

。。它将生成与上面相同的查询。我不能完全确定你的型号名称。您应该相应地进行更改。

产品清单和产品库存单位(型号)之间是否有任何关系?否,
产品库存单位
产品清单
有关系。但是,我想首先选择所有与产品关系最密切的
产品_SKU
,然后选择前5个产品-这样说,我想我需要两个单独的查询@mozammil你能发布关系吗?向你展示会更容易:)更新了问题,谢谢@mozammilt谢谢你!我之所以使用第一个解决方案,仅仅是因为我的数据库基础设施。这给了我确切的预期输出!我会尽快做标记