雄辩的产品分组,根据产品的订购顺序不同

时间:2018-08-29 12:42:41

标签: laravel-5 group-by eloquent sql-order-by

我目前有以下设置:

类别,子类别,业务,增强和产品模型。

  • 一个Category有许多SubCategory
  • 一个SubCategory有许多Business
  • 一个Business可能有一个Enhancement
  • 一个Enhancement必须有一个Product

产品可以是增强型列表,高级列表或其他功能。 如果企业带来了增强型列表或高级列表,则会将其记录在增强型表中。

因此,产品#1 =高级列表。产品#2 =增强型列表。

我想要返回的是所选SubCategory的所有业务,并按附加了增强功能的产品分组。如果业务有所改善,我想随机订购。如果公司没有产品,我想按名称按ASC顺序订购。

因此,我期望的结果是获得所有业务的完整列表,以及带有溢价列表的业务,这些业务以随机顺序排在第一位。然后,所有具有增强顺序的商家都以随机顺序排列。然后,所有其他没有增强功能的企业(按字母顺序)。

我目前已经按照名称顺序列出了所有带有以下代码的企业:

SubCategory::where('slug', $subCategory)->where('category_id', $category->id)
    ->with(['businesses' => function($query) {
        $query->orderBy('name', 'ASC');
}])->firstOrFail();

类别模型:

class Category extends Model
{   
    public function subCategories()
    {
        return $this->hasMany('\App\SubCategory');
    }
}

SubCategory模型:

class SubCategory extends Model
{

    public function businesses()
    {
        return $this->hasMany('\App\Business');
    }

    public function category()
    {
        return $this->belongsTo('\App\Category');
    }
}

商业模式:

class Business extends Model
{

    public function subCategory()
    {
        return $this->belongsTo('\App\SubCategory');
    }

    public function enhancements()
    {
        return $this->hasMany('\App\Enhancement');
    }
}

增强模型:

class Enhancement extends Model
{
    public function business()
    {
        return $this->belongsTo('\App\Business');
    }

    public function product()
    {
        return $this->belongsTo('\App\Product');
    }
}

产品型号:

class Product extends Model { }

迁移:

Schema::create('categories', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name')->unique();
    $table->string('slug')->unique();
    $table->string('featured_image');
    $table->text('description');
    $table->unsignedInteger('sort_order')->nullable();
    $table->timestamps();
    $table->softDeletes();
});


Schema::create('sub_categories', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('category_id');
    $table->string('name')->unique();
    $table->string('slug')->unique();
    $table->string('featured_image');
    $table->text('description')->nullable();
    $table->unsignedInteger('sort_order')->nullable();
    $table->timestamps();
    $table->softDeletes();

    $table->foreign('category_id')->references('id')->on('categories');
});


Schema::create('businesses', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('resort_id');
    $table->unsignedInteger('sub_category_id');
    $table->string('name');
    $table->string('slug')->unique();
    $table->string('logo')->nullable();
    $table->text('description');
    $table->text('content')->nullable();
    $table->string('county')->nullable();
    $table->string('postcode')->nullable();

    $table->timestamps();

    $table->foreign('resort_id')->references('id')->on('resorts');
    $table->foreign('sub_category_id')->references('id')->on('sub_categories');
});


Schema::create('enhancements', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('business_id');
    $table->unsignedInteger('product_id');
    $table->dateTime('from_date');
    $table->dateTime('to_date');
    $table->timestamps();
    $table->softDeletes();

    $table->foreign('business_id')->references('id')->on('businesses');
    $table->foreign('product_id')->references('id')->on('products');
});


Schema::create('products', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('slug')->unique();
    $table->string('image');
    $table->text('description');
    $table->float('yearly_price');
    $table->float('monthly_price');
    $table->timestamps();
});

1 个答案:

答案 0 :(得分:1)

这应该对您有用:

    
SubCategory::where('slug', $subCategory)
    ->where('category_id', $category->id)
    ->with(['businesses' => function($query) {
        $query->select('businesses.*')
            ->leftJoin('enhancements', function($join) {
                $join->on('enhancements.business_id', '=', 'businesses.id')
                    ->join('products', 'products.id', 'enhancements.product_id');
            })
            ->orderByRaw('FIELD(products.slug, ?, ?) DESC', ['enhanced-listing', 'premium-listing'])
            ->orderByRaw('CASE WHEN products.slug IS NOT NULL THEN RAND() ELSE businesses.name END');
    }])->firstOrFail();