基于关系获取 Collection 中 Laravel 模型的列表和计数

Getting a List And Count of Laravel Models In A Collection Based On A Relationship

我有两个 Laravel Eloquent 模型,ProductProductCategory 具有从产品到产品类别的名为 Category()belongsTo() 关系。

我已经有了模型 collection 在显示 @foreach 循环中迭代 blade 但我希望能够根据Category() 关系...

IE:在 collection 中的 20 个模型的结果集中,可能有 5 个电机、5 个变速箱和 5 个 Acme Widget。我正在尝试找到一种方法来输出作为结果一部分返回的类别列表以及与每个返回类别相关的记录数。

产品型号

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model {
/**
 * The table associated with the model.
 *
 * @var string
 */
protected $table = 'products';


/**
 * The primary key associated with the table.
 *
 * @var string
 */
protected $primaryKey = 'id';


/**
 * Indicates if the IDs are auto-incrementing.
 *
 * @var bool
 */
public $incrementing = true;


/**
 * All related features for the current model.
 */
public function features() {
    return $this->hasMany('App\ProductFeature');
}


/**
 * Created By Relationship
 */
public function created_by() {
    return $this->belongsTo('App\User');
}


/**
 * Modified By Relationship.
 */
public function modified_by() {
    return $this->belongsTo('App\User');
}


/**
 * Category Relationship.
 */
public function category() {
    return $this->belongsTo('App\ProductCategory', 'category_id', 'id');
}





/**
 * Images Relationship.
 */
public function images() {
    return $this->HasMany('App\ProductImage', 'product_id', 'id');
}


/**
 * The relations to eager load on every query.
 *
 * @var array
 */
protected $with = ['features', 'created_by', 'modified_by', 'images'];
}

产品类别模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ProductCategory extends Model {
/**
 * The table associated with the model.
 *
 * @var string
 */
protected $table = 'product_categories';


/**
 * The primary key associated with the table.
 *
 * @var string
 */
protected $primaryKey = 'id';


/**
 * Indicates if the IDs are auto-incrementing.
 *
 * @var bool
 */
public $incrementing = true;


/**
 * All related features for the current model.
 */
public function products() {
    return $this->hasMany('App\Product', 'category_id', 'id');
}


/**
 * Created By Relationship
 */
public function created_by() {
    return $this->belongsTo('App\User');
}


/**
 * Modified By Relationship.
 */
public function modified_by() {
    return $this->belongsTo('App\User');
}

}

控制器方法

public function showCategory($catAlias) {
    $products= Product::all();

    return view("website.store.results", ["products" => $products]);
}

所需的示例输出

Category | Count
=================
Cat 1    |   4
Cat 2    |   1
Cat 5    |   5

您可能希望 return 分类而不是产品。您可以使用 withCount:

方法预先加载关系的计数
public function someMethod()
{
    return view('someview', [
        'categories' => Category::withCount('products')->get(),
    ]);
}

@foreach ($categories as $category)
  <tr>
    <td>{{ $category->name }}</td>
    <td>{{ $category->products_count }}</td>
  </tr>
@endforeach

Laravel 6.x Docs - Eloquent - Relationships - Counting Related Models withCount

我不会一次性 return 购买所有产品,但如果您这样做了,您可以按 category_id 对它们进行分组,然后在必要时进行计数。

return view('someview', [
    'products' => Product::with('category:id,name')->get(),
]);

然后在需要计数的那一边:

@foreach ($products->groupBy('category_id') as $group)
  <tr>
    <td>{{ $group->first()->category->name }}</td>
    <td>{{ $group->count() }}</td>
  </tr>
@endforeach

或者您可以 return 您的产品查询并使用另一个查询来计算类别。