Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Call to a member function take() on array Laravel foreach blade

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;">{{ \Carbon\Carbon::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;">{{ \Carbon\Carbon::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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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;">{{ \Carbon\Carbon::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;">{{ \Carbon\Carbon::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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading