skip to Main Content

I am writing a laravel code in which I need a nested loop in the blade.

Here is my controller

  public function show(string $id)
  {
    $transaction = Transaction::findOrFail($id);
    $subject_ids = DB::connection('transfer')
      ->table('subjects')
      ->join('transactions_subjects', 'transactions_subjects.subject_id', '=', 'subjects.id')
      ->where('transactions_subjects.transaction_id', '=',  $transaction->id)
      ->distinct()
      ->get(['subject_id', 'name', 'hours']);

    $subjects = DB::connection('transfer')
      ->table('subjects')
      ->join('transactions_subjects', 'transactions_subjects.transferable_id', '=', 'subjects.id')
      ->where('transactions_subjects.transaction_id', '=',  $transaction->id);

    return view('transfer.transactions.details', [
      'transaction'   => $transaction,
      'subject_ids'   => $subject_ids,
      'subjects'      => $subjects,
    ]);
  }

I have a transactions table which has a subject id and a transferable id which is turn is associated with subjects table.

There is also a transaction_subject table which contains many transferables associated with one or more subject.

I broght the distinct values for each transferable so that I can loop through sujects using nested loop.

  @foreach ($subject_ids as $id)
    <div class="row wrapper table-responsive table text-center">
      <div class="col-2">
        <div>{{ $id->subject_id }}</div>
      </div>
      <div class="col-2">
        <div>{{ $id->hours }}</div>
      </div>
      @foreach ($subjects->where('subject_id', $id->subject_id)->get() as $subject)
        <div class="col-2">
          <div>{{ $subject->code }}</div>
        </div>
        <div class="col-2">
          <div>{{ $subject->name }}</div>
        </div>
        <div class="col-2">
          <div>{{ $subject->hours }}</div>
        </div>
        <div class="col-2"></div>
        <div class="col-2"></div>
        <div class="col-2"></div>
      @endforeach
      <hr>
    </div>
  @endforeach

In the blade file I made an iteration of each distinct value of transferables and inside a nested loop, I bring the associated subjects.

However, the nested loop brings the related subjects for the only first iteration of the outer loop, and there is no values for the subsequent rows.

I wrote the same logic with vanila PHP and it works fine. I must have done something wrong or missing something.

Would anybody help me with that please?

2

Answers


  1. modify codes like that:

    public function show(string $id)
    {
      $transaction = Transaction::with('subjects')->findOrFail($id);
    
      return view('transfer.transactions.details', [
        'transaction' => $transaction,
      ]);
    }
    
    @foreach ($transaction->subjects as $subject)
      <div class="row wrapper table-responsive table text-center">
        //rows
      </div>
    @endforeach
    

    in your model:

    public function subjects()
    {
      return $this->belongsToMany(Subject::class, 'transactions_subjects', 'transaction_id', 'subject_id');
    }
    
    Login or Signup to reply.
  2. the issue is how you are fetching the subjects directly in the loop.

    There are 2 more ways you can get the related data more better than this approach, one is through model relationships or joins but anyhow change your code this.

    @foreach ($subject_ids as $id)
        {{--  your html code    --}}
        @php
            $relatedSubjects = $subjects->where('subject_id', $id->subject_id);
        @endphp
        @foreach ($relatedSubjects as $subject)
          {{--  your html code    --}}
        @endforeach
    @endforeach
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search