我有一个关于Laravel口才的问题。
数据库表
类别:id root_id名称
产品ID名称等。
product_categories id product_id category_id
因此它可能是具有子类别B的CategoryA,而类别B本身具有子类别C。
当我单击CategoryA时,我想查找属于CategoryA,CategoryB,CategoryC的所有产品
Category Model
public function cats()
{
return $this->hasMany(Category::class);
}
public function childrenCategories()
{
return $this->hasMany(Category::class)->with('cats');
}
产品型号
public function categories()
{
return $this->belongsToMany(Category::class);
}
控制器
//首先,我得到所有类别的所有ID,所有级别的子类别。
$categories = Category::where('category_id',$category_id)->with('childrenCategories')->get();
$all_cat_array=array();$all_prod=array();
foreach ($categories as $category)
{
foreach ($category->childrenCategories as $childCategory) {
array_push($all_cat_array, $childCategory->id);
}
array_push($all_cat_array,$category->id);
}
//然后我得到了产品的所有ID
foreach ($all_cat_array as $cat)
{
if(CategoryProduct::where('category_id',$cat)->exists()) {
$prod=CategoryProduct::where('category_id',$cat)->pluck('product_id');
array_push($all_prod,$prod );
}
}
但是我不想使用所有这些foreach,因为我想优化代码。 我可以做些什么来简化它?
答案 0 :(得分:1)
有关更多信息,请阅读Laravel Docs
数据库
类别数据库
Schema::create('category_as', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('product_id');
...
$table->timestamps();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
});
类别B数据库
Schema::create('category_bs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('category_a_id');
...
$table->timestamps();
$table->foreign('category_a_id')->references('id')->on('category_as')->onDelete('cascade');
});
模型
产品型号
public function categories_a() {
return $this->hasmany(Category_a::class);
}
类别A模型
public function categories_b() {
return $this->hasmany(Category_b::class);
}
public function product() {
return $this->belongsTo(Product::class);
}
B类模型
public function category_a() {
return $this->belongsTo(Category_a::class);
}
public function clients() {
return $this->hasManyThrough(Client::class, Code::class);
}
CONTROLLER
public function index() {
$products = Product::all();
$query = $products->categories_a->categories_b;
dd($query);
}
已编辑
数据库
类别数据库
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('product_id');
...
$table->timestamps();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
});
模型
产品型号
public function categories() {
return $this->hasmany(Category::class);
}
类别模型
public function product() {
return $this->belongsTo(Product::class);
}
CONTROLLER
public function index() {
$products = Product::all();
$query = $products->categories;
dd($query);
}
答案 1 :(得分:0)
我建议您在类别模型中使用嵌套集结构。
此软件包https://github.com/lazychaser/laravel-nestedset中的实现很好。
通过2个查询,您可以获得类别及其所有后代的产品(具有无限嵌套)。
如果类别属于许多产品:
$categoryIds = Category::descendantsAndSelf($categoryId)->pluck('id');
$products = Product::whereHas('categories', function ($query) use ($categoryIds) {
$query->whereIn('categories.id', $categoryIds);
});
如果类别中有很多产品
$categoryIds = Category::descendantsAndSelf($categoryId)->pluck('id');
$products = Product::whereIn('category_id', $categoryIds);