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

时间:2019-12-10 09:41:37

标签: php laravel eloquent eloquent--relationship

我有两个Laravel Eloquent模型,ProductProductCategory,从产品到ProductCategory的belongsTo()关系为Category()

我已经有一个模型集合通过显示刀片中的@foreach循环进行迭代,但是希望能够基于Category()关系来获得产品数量的计数... < / p>

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

产品型号

<?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

1 个答案:

答案 0 :(得分:1)

您可能想返回类别而不是产品。您可以使用方法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

我不会一口气退还所有产品,但是如果您愿意,可以按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

或者您可以返回您的产品查询,并使用另一个查询来查询其类别的计数。

相关问题