Sulav Lal Khosin
Sulav Lal Khosin

Reputation: 15

Is there "Laravel" way for executing following code?

I have used various loop for storing data in array which I believe is the old fashioned way. Later these arrays are again used for displaying data. The loop I used is as follows

foreach ($adsources as $adsource) {
    $adsou[$x] = $adsource->newspaper;
    $count[$x] = '0';
    $areac[$x] = '0';
    $areab[$x] = '0';

    foreach ($advert as $ad) {                
        if ($ad->adsource_id == $adsource->id) {
            $count[$x]++;
            $cctemp = ($ad->c) * ($ad->cm);
            if ($ad->color=='0') {
                $areac[$x] = $areac[$x] + $cctemp;
            } else {
                $areab[$x] = $areab[$x] + $cctemp;
            }
        }
        if ($count!='0') {
            $totalcc[$x] = $areac[$x] + $areab[$x];
        } else {
            $totalcc[$x] = '0';
        }
    }
    $x++;
}

The view table is as follows

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

I have defined following variables in the controller

$adsources = Adsource::all();
$advert = Advertisement::all();
$x = '0';
$areac = array();
$areab = array();
$totalcc = array(); 
$cctemp = '0'; 
$adsou = array(); 
$count = array(); 

and returned view as follows

    return view('pages.report.reportbysource', compact('adsou', 'count', 'areac', 'areab', 'totalcc', 'x'));
} 

Advertisement table contains(id, date, adsource_id, color, c, cm)

adsource_id is foreign key; the table contains(id, newspaper)

The function in controller looks like this

public function reportBySource()
{        
    $adsources = Adsource::all();
    $advert = Advertisement::all();
    $x = '0';
    $areac = array();
    $areab = array();
    $totalcc = array();
    $cctemp = '0';
    $adsou = array();
    $count = array();
    
    foreach ($adsources as $adsource) {
        $adsou[$x] = $adsource->newspaper;
        $count[$x] = '0';
        $areac[$x] = '0';
        $areab[$x] = '0';

        foreach ($advert as $ad) {                
            if ($ad->adsource_id == $adsource->id) {
                $count[$x]++;
                $cctemp = ($ad->c) * ($ad->cm);
                if ($ad->color == '0') {
                    $areac[$x] = $areac[$x] + $cctemp;
                } else {
                    $areab[$x] = $areab[$x] + $cctemp;
                }
            }

            if ($count != '0') {
                $totalcc[$x] = $areac[$x] + $areab[$x];
            } else {
                $totalcc[$x] = '0';
            }
        }
        $x++;
    }        

    return view('pages.report.reportbysource', compact('adsou', 'count', 'areac', 'areab', 'totalcc', 'x'));
}

Upvotes: 0

Views: 43

Answers (1)

IGP
IGP

Reputation: 15786

Based on your question, I'm assuming there's a relationship between Adsource and Advertisement

# Adsource model
public function advertisements()
{
    return $this->hasMany(Advertisement::class, 'adsource_id');
}
# Advertisement model
public function adsource()
{
    return $this->belongsTo(Adsource::class, 'adsource_id');
}

Next, I think $areab, $areac and $totalcc look like something that could go some accessor methods

# Advertisement model
// Usage: $advertisement->cc
public function getCcAttribute()
{
    return $this->c * $this->cm;
}
# Adsource model
// Usage: $adsource->total_area_color
public function getTotalAreaColorAttribute()
{
    return $this->advertisements->where('color', '0')->sum('cc');
}

// Usage: $adsource->total_area_bw
public function getTotalAreaBwAttribute()
{
    return $this->advertisements->where('color', '!=', '0')->sum('cc');
}

// Usage: $adsource->total_area
public function getTotalAreaAttribute()
{
    return $this->advertisements->sum('cc');
}

With that out of the way, I don't think you need any logic in the controller

# Controller
public function reportBySource()
{        
    $adsources = Adsource::with('advertisements')->get();

    return view('pages.report.reportbysource', compact('adsources'));
}
<thead class="thead-dark">
    <tr>
        <th>Newspaper Name</th>
        <th>Count</th>
        <th>Area[Color]</th>
        <th>Area[B&W]</th>
        <th>Total Area(cc)</th>
    </tr>
</thead>
<tbody>                                               
    @foreach ($adsources as $adsource)
        <tr>
            <td>{{ $adsource->newspaper }}</td>
            <td>{{ $adsource->advertisements->count() }}</td>
            <td>{{ $adsource->total_area_color}}</td>
            <td>{{ $adsource->total_area_bw }}</td>
            <td>{{ $adsource->total_area }}</td>
        </tr>
    @endforeach
</tbody>

Upvotes: 1

Related Questions