skip to Main Content

I have a problem, I would like to view only the last 7 results of this collection but it gives me this error.
Have any of you ever experienced a similar error?

  @foreach ($audits->take(-7) as $item) 
    @if ($item->esito === 'POSITIVO') 
        <div style="display: flex; flex-direction:column;">
          <span class="audit-daily-text39">
            <strong style="font-size: 13px;">{{ CarbonCarbon::parse($item->data_audit)->format('d/m') }}</strong>
          </span>
          <div class="audit-daily-frame4364">
            <div class="audit-daily-day-ko">
              <div class="audit-daily-cell">
                <div class="mb-2">
                  <i class="align-middle me-2 fas fa-fw fa-check" style="color: white; position: relative; top: 6px; left: 4px;"></i>
                </div>
              </div>
            </div>
          </div>
        </div> @elseif ($item->esito === 'NEGATIVO') <div style="display: flex; flex-direction:column;">
          <span class="audit-daily-text39">
            <strong style="font-size: 13px;">{{ CarbonCarbon::parse($item->data_audit)->format('d/m') }}</strong>
          </span>
          <div class="audit-daily-frame4364">
            <div class="audit-daily-day-ko">
              <div class="audit-daily-cell" style="background-color: red">
                <div class="mb-2">
                  <i class="align-middle me-2 fas fa-fw fa-close" style="color: white; position: relative; top: 6px; left: 4px;"></i>
                </div>
              </div>
            </div>
          </div>
        </div> 
    @endif 
@endforeach

This is Controller

 if (DB::table('esiti_audit')->where('employee', $name)->doesntExist()) {

                $data = "0";
            } elseif (DB::table('esiti_audit')->where('employee', $name)->exists()) {

                $audits = DB::table('esiti_audit')->where('employee', $name)->where('frequency_audit', "DAY")->select(['master_zone', 'subarea', 'microarea', 'data_audit', 'esito'])
                    ->orderBy('master_zone', 'asc')->orderBy('subarea', 'asc')->orderBy('microarea', 'asc')->orderBy('data_audit', 'asc')->get()->toArray();

                $data = [];

                foreach ($audits as $audit) {

                    $areaKey =  "{$audit->master_zone} {$audit->subarea} {$audit->microarea}";

                    $data[$areaKey][] = [
                        'esito' => $audit->esito,
                        'data_audit' => $audit->data_audit
                    ];
                };
            };

this is the controller code, know that the foreach audits loop is a nested loop above @foreach ($data as $area => $audits)
@endforeach

2

Answers


  1. change your audits to this

    $audits = DB::table('esiti_audit')->where('employee', $name)->where('frequency_audit', "DAY")->select(['id', 'master_zone', 'subarea', 'microarea', 'data_audit', 'esito'])
        ->orderBy('master_zone', 'asc')
        ->orderBy('subarea', 'asc')
        ->orderBy('microarea', 'asc')
        ->orderBy('data_audit', 'asc')
        ->latest('id')
        ->take(7)
        ->get()
        ->toArray();
    

    You don’t need this in your blade, remove the take()

    @foreach ($audits->take(-7) as $item) 
    
    Login or Signup to reply.
  2. It looks like you are trying to access the array as a collection object, I can see that from the controller you are constructing $data and passing it down to blade view as $audits.

    The following should help you:

      @foreach (array_slice($audits, -7) as $item) 
        @if ($item['esito'] === 'POSITIVO') 
            <div style="display: flex; flex-direction:column;">
              <span class="audit-daily-text39">
                <strong style="font-size: 13px;">{{ CarbonCarbon::parse($item['data_audit'])->format('d/m') }}</strong>
              </span>
              <div class="audit-daily-frame4364">
                <div class="audit-daily-day-ko">
                  <div class="audit-daily-cell">
                    <div class="mb-2">
                      <i class="align-middle me-2 fas fa-fw fa-check" style="color: white; position: relative; top: 6px; left: 4px;"></i>
                    </div>
                  </div>
                </div>
              </div>
            </div> @elseif ($item['esito'] === 'NEGATIVO') <div style="display: flex; flex-direction:column;">
              <span class="audit-daily-text39">
                <strong style="font-size: 13px;">{{ CarbonCarbon::parse($item['data_audit'])->format('d/m') }}</strong>
              </span>
              <div class="audit-daily-frame4364">
                <div class="audit-daily-day-ko">
                  <div class="audit-daily-cell" style="background-color: red">
                    <div class="mb-2">
                      <i class="align-middle me-2 fas fa-fw fa-close" style="color: white; position: relative; top: 6px; left: 4px;"></i>
                    </div>
                  </div>
                </div>
              </div>
            </div> 
        @endif 
      @endforeach
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search