查询联接表中选定数据的计数

时间:2018-10-05 09:59:40

标签: mysql laravel join

我正在尝试获取product_categories和过滤器的所有组合,以及每种产品选择的过滤器数量。

我可以得到组合,但是我很难获得正确的计数。 我正在Laravel中使用\ DB :: raw查询。

这是我到目前为止所得到的:

\DB::raw("SELECT product_categories.*,filters.name as filter, filter_types.heading as filter_type
                FROM product_categories, filters
                JOIN filter_types ON filter_types.id = filters.filter_type_id
                WHERE filter_types.type = 'Modules\\\DonaldRussell\\\ProductCategories\\\App\\\ProductCategory'
                ORDER BY product_categories.name, filter_type
                ");

数据库架构如下:

Schema::create('filter_types', function(Blueprint $table) {
        $table->increments('id');
        $table->string('type')->notNull();
        $table->string('heading')->notNull();
        $table->timestamps();
    });

Schema::create('filters', function(Blueprint $table) {
        $table->increments('id');
        $table->integer('filter_type_id')->unsigned()->notNull();
        $table->foreign('filter_type_id')->references('id')->on('categories_types');
        $table->string('name')->notNull();
        $table->integer('order')->notNull();
        $table->integer('uses')->unsigned()->notNull()->default(0);
        $table->timestamps();
    });

Schema::create('product_categories', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name')->notNull();
        $table->string('slug')->notNull();
        $table->integer('order')->notnull()->unsigned()->default(0);
        $table->integer('type')->notnull()->unsigned()->default(1);
        $table->integer('seo')->nullable()->unsigned();
        $table->foreign('seo')->references('id')->on('seos')->onDelete('cascade');
        $table->timestamps();
    });

Schema::create('product_category_sets', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('category_id')->nullable()->unsigned();
        $table->foreign('category_id')->references('id')->on('product_categories')->onDelete('set null');
        $table->integer('sub_category_id')->nullable()->unsigned();
        $table->foreign('sub_category_id')->references('id')->on('tags')->onDelete('set null');
        $table->integer('sub_sub_category_id')->nullable()->unsigned();
        $table->foreign('sub_sub_category_id')->references('id')->on('tags')->onDelete('set null');
        $table->integer('product_id')->notnull()->unsigned();
        $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
        $table->timestamps();
    });

Schema::create('product_category_set_filters', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('product_category_set_id')->notnull()->unsigned();
        $table->foreign('product_category_set_id')->references('id')->on('product_category_sets')->onDelete('cascade');
        $table->integer('filter_id')->notnull()->unsigned();
        $table->foreign('filter_id')->references('id')->on('filters')->onDelete('cascade');
        $table->timestamps();
    });

在下面的屏幕截图中,在没有count列的情况下看不到我需要的结果: enter image description here

1 个答案:

答案 0 :(得分:0)

首先,我认为您的结构不正确,但让我们假设它是正确的。

我们需要4种型号: ProductCategorySet::classProduct::classCategory::class(product_category), 和Filter::class

有了它们之间的适当关系,您将能够做到

ProductCategorySet::with('product')->with('category')->withCount('filters')->get();

这就是您所需要的。

use Illuminate\Database\Eloquent\Model;

class ProductCategorySet extends Model
{
    public function product()
    {
        return $this->belongsTo(Product::class);
    }

    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    public function filters()
    {
        return $this->belongsToMany(Filter::class, 'product_category_set_filters', 'product_category_set_id', 'filter_id');
    }
}