Laravel Nested @foreach By 2 Different Model in View

梦想与她 提交于 2021-01-20 13:51:36

问题


Model

// Relationship in \App\FatherRegistrars and \App\MotherRegistrars and \App\GuardianMaleRegistrars \App\GuardianFemaleRegistrars
public function student_registrars()
{
    return $this->belongsToMany('App\StudentRegistrars')->withTrashed();
}

Controller

public function index(Request $request)
{
    $dataFathers = \App\FatherRegistrars::get();
    $dataMothers = \App\MotherRegistrars::get();

    $dataGM = \App\GuardianMaleRegistrars::get();
    $dataGF = \App\GuardianFemaleRegistrars::get();

    // manual pagination using code attached in AppServiceProvider.php
    $data = $dataFathers->toBase()->merge($dataMothers)->paginate($items);

    return view('parents-guardians-registrars.index', compact('data', 'dataFathers', 'dataMothers', 'dataGM', 'dataGF'))->withItems($items); 
}

View

@foreach($data as $var)
    <tr>
        <td style="text-align:center;">
          <input type="checkbox" id="select" class="sub_chk" data-id="{{$var->id}}" value="{{$var->id}}" name="selected_values[]"/>
        </td>
        <td>{{$var->id }}</td>
        <td>{{$var->name}}</td>
        <td>{{$var->email}}</td>
        <td> 
          <?php $elements = array(); ?>
          @foreach($var->student_registrars as $category)
            <?php $elements[] = '<a href=" '.route('student-registrars.show', $category->id).' "> '.$category->name.' </a>'; ?>
          @endforeach
          <?php echo implode(',<br>', $elements); ?>
        </td> 
        <td>
          // Second foreach should be here
        </td>
        <td>
          <a href="{{ route('father-registrars.show', $var->id) }}" class="btn btn-primary btn-sm">Detail</a>
        </td>
    @endforeach 

    // Second foreach
    @foreach($dataGM as $var2)
    <tr>
       <td>
         <?php $elements = array(); ?>
         @foreach($var2->student_registrars as $category)
            <?php $elements[] = '<a href=" '.route('student-registrars.show', $category->id).' "> '.$category->name.' </a>'; ?>
         @endforeach
         <?php echo implode(',<br>', $elements); ?>
       </td>
    </tr>
    @endforeach 

      </tr>

And the result for code above is:

I have a little problem related multiple foreach in one view. It's actually just a simple problem but I am stuck here. Any body can solve it? Should I use partial view to do this?


回答1:


This may not exactly work (not tested or verified), but you can give something like this a try:

@foreach($data as $var)
  <tr>
    <td style="text-align:center;">
      <input type="checkbox" id="select" class="sub_chk" data-id="{{$var->id}}" value="{{$var->id}}" name="selected_values[]"/>
    </td>
    <td>{{$var->id }}</td>
    <td>{{$var->name}}</td>
    <td>{{$var->email}}</td>

    <?php $elements = array(); ?>
    @foreach($var->student_registrars as $i => $category)
      <td> 
        <?php $elements[] = '<a href=" '.route('student-registrars.show', $category->id).' "> '.$category->name.' </a>'; ?>
        <?php echo implode(',<br>', $elements); ?>
      </td> 
      <td>
          <?php $elements2 = array(); ?>
         @foreach($var2->student_registrars[$i] as $category)
            <?php $elements2[] = '<a href=" '.route('student-registrars.show', $category->id).' "> '.$category->name.' </a>'; ?>
         @endforeach
         <?php echo implode(',<br>', $elements2); ?>
      </td>
    @endforeach
    
    <td>
      <a href="{{ route('father-registrars.show', $var->id) }}" class="btn btn-primary btn-sm">Detail</a>
    </td>
  </tr>
@endforeach 

The problem you have is that there is no direct association $data and $dataGM. You should try and use Relationships so that the data needed for $dataGM can be accessed as a relationship from $data, e.g.

$children = $data[$i]->children;
$adopted = $data[$i]->adopted_children;

Then you can loop directly on these items instead of having to create multiple variables.



来源:https://stackoverflow.com/questions/65454848/laravel-nested-foreach-by-2-different-model-in-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!