I am beginner in Laravel and php. This is my first post on SO :)
I use in my project Laravel 5.8. I try generate raport.
I have this code:
class Dish extends Model
{
protected $quarded = ['id'];
protected $fillable = ['company_id', 'name', 'description', 'enable', 'allergen'];
public $timestamps = false;
public function components()
{
return $this->hasManyThrough('App\DishValues', 'App\Dish', 'id', 'dishes_id');
}
public function foodIngredient()
{
return $this->hasManyThrough('App\FoodIngredient', 'App\DishValues', 'dishes_id', 'id');
}
}
class DishValues extends Model
{
protected $quarded = ['id'];
protected $fillable = ['dishes_id', 'food_ingredient_id', 'quantity'];
public $timestamps = false;
public function ingredient()
{
return $this->belongsTo('App\FoodIngredient', 'food_ingredient_id');
}
}
class FoodIngredient extends Model
{
use scopeActiveTrait;
public function scopeVisibleDemo($query)
{
return $query->where('available_in_demo', 1);
}
protected $quarded = ['id'];
protected $fillable = ['company_id', 'name', 'garbage', 'energy_value', 'protein', 'fat', 'available_carbohydrates', 'roughage', 'description', 'url_address', 'allergen', 'available_in_demo', 'enable'];
public $timestamps = false;
}
class NutritionPlan extends Model
{
protected $quarded = ['id'];
protected $fillable = ['norm1', 'norm2', 'norm3', 'norm4', 'norm5', 'kids_0_1', 'company_id', 'date_from', 'date_to', 'kids_1_3', 'kids_4_6', 'kids_7_9', 'boy_10_12', 'boy_13_15', 'boy_16_18', 'boy_19_30', 'girl_10_12', 'girl_13_15', 'girl_16_18', 'girl_19_30', 'institution'];
public $timestamps = false;
public function items()
{
return $this->hasManyThrough('App\NutritionPlanDish', 'App\NutritionPlan', 'id', 'nutrition_plans_id');
}
}
class NutritionPlanDish extends Model
{
protected $quarded = ['id'];
protected $fillable = ['company_id', 'nutrition_plans_id', 'dish_id', 'date', 'meal_type'];
public $timestamps = false;
public function dish()
{
return $this->belongsTo('App\Dish', 'dish_id');
}
}
My function to generate
public function generateReport(int $nutritionId, $request)
{
return NutritionPlan::with(['items'])->where('company_id', '=', Auth::user()->company_id)->where('id', $nutritionId)->first();
}
And my blade file:
<table width="100%">
<thead style="background-color: lightgray;">
<tr>
<th width="80%" align="center" scope="col"><b>Product name</b></th>
<th width="20%" align="center" scope="col"><b>How many</b></th>
</tr>
</thead>
@php
Illuminate\Support\Carbon::setLocale('pl');
$period = Carbon\CarbonPeriod::create($nutritionPlan->date_from, $nutritionPlan->date_to);
@endphp
@foreach ($period as $date)
<tr>
<td colspan="2" class="headerTableItemTd">
<div class="headerTable">,
</div>
</td>
</tr>
<tr>
<td colspan="2" class="headerTableItemTd">
<div class="headerTableItem"><u>Śniadanie 1</u></div>
</td>
</tr>
@foreach($nutritionPlan->items as $value)
@if($value->meal_type == 1 and $value->date == $date->format('Y-m-d'))
<tr>
<td width="80%" class="fontNormal td2 tdBorderRight tdBorderBottom"
style="vertical-align: top;">
<div class="tablePadding">
@if(isset($value->dish->components))
@foreach($value->dish->components as $items)
<br/>
@endforeach
@endif
</div>
</td>
<td width="20%" class="fontNormal td2 tdBorderRight tdBorderBottom"
style="vertical-align: top;">
<div class="tablePadding">
@if(isset($value->dish->components))
@foreach($value->dish->components as $items)
<br/>
@endforeach
@endif
</div>
</td>
</tr>
@endif
@endforeach
@endforeach
</table>
It's work fine, but in my result i have:
name 1 - 10
name 2 - 20
name 3 - 30
name 4 - 40
name 1 - 1
name 2 - 2
name 3 - 3
name 4 - 4
I want group my result and show summary:
name 1 - 11
name 2 - 22
name 3 - 33
name 4 - 44
How can i make it?
I would like to display grouped results (one name and sum of quantities) - no duplicates.
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2RsmcDR
via IFTTT
Aucun commentaire:
Enregistrer un commentaire