Php “有没有?”;“拉雷维尔”;执行以下代码的方法?

Php “有没有?”;“拉雷维尔”;执行以下代码的方法?,php,laravel,laravel-7,Php,Laravel,Laravel 7,我使用了各种循环来存储数组中的数据,我认为这是一种老式的方式。稍后,这些数组再次用于显示数据。 我使用的循环如下 <thead class="thead-dark"> <th>Newspaper Name</th> <th>Count</th> <th>Area[Color]</th> <th>Area[B&W]</th>

我使用了各种循环来存储数组中的数据,我认为这是一种老式的方式。稍后,这些数组再次用于显示数据。 我使用的循环如下

<thead class="thead-dark">
    <th>Newspaper Name</th>
    <th>Count</th>
    <th>Area[Color]</th>
    <th>Area[B&W]</th>
    <th>Total Area(cc)</th>
</thead>
<tbody>                                               
    @for ($i = 0; $i < $x; $i++)
        <tr>
            <td>{{ $adsou[$i] }}</td>
            <td>{{ $count[$i] }}</td>
            <td>{{ $areac[$i] }}</td>
            <td>{{ $areab[$i] }}</td>
            <td>{{ $totalcc[$i] }}</td>
        </tr>
    @endfor
</tbody>
    return view('pages.report.reportbysource', compact('adsou', 'count', 'areac', 'areab', 'totalcc', 'x'));
} 
foreach($adsources作为$adsource){
$adsou[$x]=$adsource->报纸;
$count[$x]=“0”;
$areac[$x]=“0”;
$areab[$x]=“0”;
foreach($advert作为$ad){
如果($ad->adsource\U id==$adsource->id){
$count[$x]++;
$cctemp=($ad->c)*($ad->cm);
如果($ad->color='0'){
$areac[$x]=$areac[$x]+$cctemp;
}否则{
$areab[$x]=$areab[$x]+$cctemp;
}
}
如果($count!=“0”){
$totalcc[$x]=$areac[$x]+$areab[$x];
}否则{
$totalcc[$x]=“0”;
}
}
$x++;
}
视图表如下所示

<thead class="thead-dark">
    <th>Newspaper Name</th>
    <th>Count</th>
    <th>Area[Color]</th>
    <th>Area[B&W]</th>
    <th>Total Area(cc)</th>
</thead>
<tbody>                                               
    @for ($i = 0; $i < $x; $i++)
        <tr>
            <td>{{ $adsou[$i] }}</td>
            <td>{{ $count[$i] }}</td>
            <td>{{ $areac[$i] }}</td>
            <td>{{ $areab[$i] }}</td>
            <td>{{ $totalcc[$i] }}</td>
        </tr>
    @endfor
</tbody>
    return view('pages.report.reportbysource', compact('adsou', 'count', 'areac', 'areab', 'totalcc', 'x'));
} 
并返回如下视图

<thead class="thead-dark">
    <th>Newspaper Name</th>
    <th>Count</th>
    <th>Area[Color]</th>
    <th>Area[B&W]</th>
    <th>Total Area(cc)</th>
</thead>
<tbody>                                               
    @for ($i = 0; $i < $x; $i++)
        <tr>
            <td>{{ $adsou[$i] }}</td>
            <td>{{ $count[$i] }}</td>
            <td>{{ $areac[$i] }}</td>
            <td>{{ $areab[$i] }}</td>
            <td>{{ $totalcc[$i] }}</td>
        </tr>
    @endfor
</tbody>
    return view('pages.report.reportbysource', compact('adsou', 'count', 'areac', 'areab', 'totalcc', 'x'));
} 
广告表包含(id、日期、广告源id、颜色、c、cm)

adsource\u id
是外键;该表包含(id、报纸)

控制器中的函数如下所示

公共函数reportBySource()
{        
$adsources=Adsource::all();
$advert=广告::全部();
$x='0';
$areac=array();
$areab=array();
$totalcc=array();
$cctemp='0';
$adsou=array();
$count=array();
foreach($adsources作为$adsource){
$adsou[$x]=$adsource->报纸;
$count[$x]=“0”;
$areac[$x]=“0”;
$areab[$x]=“0”;
foreach($advert作为$ad){
如果($ad->adsource\U id==$adsource->id){
$count[$x]++;
$cctemp=($ad->c)*($ad->cm);
如果($ad->color==“0”){
$areac[$x]=$areac[$x]+$cctemp;
}否则{
$areab[$x]=$areab[$x]+$cctemp;
}
}
如果($count!=“0”){
$totalcc[$x]=$areac[$x]+$areab[$x];
}否则{
$totalcc[$x]=“0”;
}
}
$x++;
}        
返回视图('pages.report.reportbysource',compact('adsou','count','areac','areab','totalcc','x');
}

基于你的问题,我假设
Adsource
广告之间存在关系

#Adsource模型
公益广告()
{
返回$this->hasMany(广告::类,'adsource_id');
}
#广告模式
公共函数adsource()
{
返回$this->belongsTo(Adsource::class,'Adsource_id');
}
接下来,我认为
$areab
$areac
$totalcc
看起来像是可以通过一些访问器方法访问的东西

#广告模式
//用法:$advision->cc
公共函数getCcAttribute()
{
返回$this->c*$this->cm;
}
#Adsource模型
//用法:$adsource->total\u area\u color
公共函数getTotaleAColorattribute()
{
返回$this->advisions->where('color','0')->sum('cc');
}
//用法:$adsource->total\u area\u bw
公共函数getTotaleAreabWatribute()
{
返回$this->advisions->where('color','!=','0')->sum('cc');
}
//用法:$adsource->总面积
公共函数getTotaleAAttribute()
{
返回$this->advisions->sum('cc');
}
这样一来,我认为控制器中不需要任何逻辑

$adsources = Adsource::all();
$advert = Advertisement::all();
$x = '0';
$areac = array();
$areab = array();
$totalcc = array(); 
$cctemp = '0'; 
$adsou = array(); 
$count = array(); 
#控制器
公共函数reportBySource()
{        
$adsources=Adsource::with('advisions')->get();
返回视图('pages.report.reportbysource',compact('adsources');
}

报名
计数
面积[颜色]
区域[B&W]
总面积(cc)
@foreach($adsources作为$adsource)
{{$adsource->paper}
{{$adsource->advisions->count()}
{{$adsource->total_area_color}
{{$adsource->total_area_bw}
{{$adsource->total_area}
@endforeach