Eloquent 按产品分组,根据产品进行不同排序
Eloquent group by product, ordering differently based on product
我目前有以下设置:
类别、子类别、业务、增强和产品模型。
- 一个
Category
有很多SubCategory
- 一个
SubCategory
有很多Business
- 一个
Business
可能有一个Enhancement
- 一个
Enhancement
必须有一个Product
产品可以是增强列表、高级列表或附加功能。
如果企业带来了增强版或高级版列表,则会记录在增强版中 table。
因此,产品#1 = 高级列表。产品 #2 = 增强列表。
我想要的是 return 所选子类别的所有业务,按附加增强功能的产品分组。如果业务有改进,我想随机化订单。如果商家没有产品,我想按名称按 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');
}
}
子类别模型:
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();
});
这应该适合你:
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();
我目前有以下设置:
类别、子类别、业务、增强和产品模型。
- 一个
Category
有很多SubCategory
- 一个
SubCategory
有很多Business
- 一个
Business
可能有一个Enhancement
- 一个
Enhancement
必须有一个Product
产品可以是增强列表、高级列表或附加功能。 如果企业带来了增强版或高级版列表,则会记录在增强版中 table。
因此,产品#1 = 高级列表。产品 #2 = 增强列表。
我想要的是 return 所选子类别的所有业务,按附加增强功能的产品分组。如果业务有改进,我想随机化订单。如果商家没有产品,我想按名称按 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');
}
}
子类别模型:
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();
});
这应该适合你:
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();