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