Сначала создайте модели по терминалу или вручную
php artisan make:model stock_reports
php artisan make:model sizes
php artisan make:model products
php artisan make:model colors
Добавьте в файл stock_reports.php в приложении-словаре
public function product()
{
return $this->belongsTo(products::class, 'product_id');
}
public function size()
{
return $this->belongsTo(sizes::class, 'size_id');
}
public function color()
{
return $this->belongsTo(colors::class, 'color_id');
}
Добавьте отчеты о функциях в products.php
public function reports()
{
return $this->hasMany(stock_reports::class, 'product_id');
}
Наконец, под вашим контролем используйте:
$stock_reports=stock_reports::
with('product')
->get()->sortBy('product.product_name')->sortBy('created_at');
foreach($stock_reports as $report)
{
$product_name=$report->product->product_name;
$size_name=$report->size->size_name;
$color_name=$report->color->color_name;
$qty_products=$report->product->reports()->count();
// use wherever you need to use it
}
Результаты:
Name Quantity CREATED_AT
T-shirt C x-Red-1 2019-09-11 18:55:12
Name Quantity CREATED_AT
T-shirt D xl-Blue-1 2019-09-11 18:55:12
Name Quantity CREATED_AT
T-shirt A xl-Blue-1 2019-09-12 18:55:12
Name Quantity CREATED_AT
T-shirt B x-White-1 2019-09-13 18:55:12
Некоторые предложения:
Использование нумерация страниц вместо -> get ()
Использовать внешние ключи