1
Good am using Laravel 5.2
and I have much doubt to count the lines related to my research. I need to return the number of products within Categorias
and SubCategorias
.
My relationship is as follows, in the table Product I have a field Cdsubcategory that references the Cdsubcategory of the table of Subcategory, besides this I have within the table of Subcategory the code of the category, this due to the relationship (one for many for category and subcategory) as in the image below.
In my model Product I made like this:
class Produto extends Model
{
use SoftDeletes;
protected $fillable = [
'CdSubCategoria',
'NmProduto',
'DscProduto',
'VlUnit',
'UnitEmEstoque',
'FlgDescontinuado',
'FlgProdutoVisivel',
'Visivel_Ini',
'Visivel_Fim',
'FlgPontua',
'QtdPontos',
'MaxPontosPorSubCategoria'
];
protected $primaryKey = 'CdProduto';
protected $dates = ['deleted_at'];
public function subCategoria()
{
return $this->belongsTo('App\SubCategoria','CdSubCategoria','CdSubCategoria');
}
public function imagens(){
return $this->hasMany('App\Imagem', 'CdProduto', 'CdProduto');
}
public function lotes(){
return $this->hasMany('App\LoteProduto', 'CdProduto', 'CdProduto');
}
public function tipo_produto_embalagem(){
return $this->belongsTo('App\TipoProdutoEmbalagem', 'CdProduto', 'CdProduto');
}
}
Already no model Categoria:
class Categoria extends Model
{
use SoftDeletes;
protected $fillable = ['NmCategoria', 'DscCategoria'];
protected $primaryKey = 'CdCategoria';
protected $dates = ['deleted_at'];
public function subCategorias()
{
return $this->hasMany('App\SubCategoria','CdCategoria','CdCategoria');
}
}
And finally in the Subcategory model:
class SubCategoria extends Model
{
use SoftDeletes;
protected $fillable = ['CdCategoria','NmSubCategoria', 'DscSubCategoria', 'FlgPontua', 'QtdPontos', 'MaxPontosCategoria'];
protected $primaryKey = 'CdSubCategoria';
protected $dates = ['deleted_at'];
public function categoria()
{
return $this->belongsTo('App\Categoria','CdCategoria','CdCategoria');
}
public function produtos(){
return $this->hasMany('App\Produto','CdSubCategoria','CdSubCategoria');
}
}
Counting the number of products within a subcategory I managed in a static way, and I did as follows:
$count = Produto::where('CdSubCategoria','3')->count();
dd($count);
But I have some problems like:
I need to count all the
subCategorias
, ie this count needs to be dynamic. It is worth remembering that I need this information in my view, because I am using it so:CATEGORY NAME (QTY PRODUCT)
I need in addition to the quantity of products per
subCategoria
It is necessary product by categories (Remembering that this relationship is one for many)
OBS.
The ideal is that this information comes together with my model. Ex: the same way I access
$produto->imagens
could access$subCategoria->qtd
and$categoria->qtd
the item 1) Name and Subcategory are in the product table?
– novic
@Virgilionovic No, as shown in the image Product so has Cdsubcategory, already within Subcategory I have her name and even the Cdcategoria.
– Renan Rodrigues
@Virgilionovic I updated my question, check if it’s understandable now ?
– Renan Rodrigues
you placed two codes of the category can add the sub-category?
– novic
Sorry I’ve already updated
– Renan Rodrigues