Reputation: 53
I have this code, and I want to sort them by order, I have the order
column in database which always takes 1, 2, 3, 4
and I want the answers to be in that order how can I do this on laravel blade .
<div class="form-group" id="group-form-id">
<label id="correct_answer" style="color: #00a65a;">
@lang('test-results.correct_answer'):
<div style="line-height: 8px; padding-top: 5px;">
<?php $i=1; ?>
@foreach($question->answers as $answer)
@if($answer->order)
<span id="test_span">
<br>
<em style="padding-right: 10px; color: #999;">
{{ $i }}.
</em>
{!! $answer->text !!}
</span>
<hr style="width: 500%;border-top: 1px solid rgba(238, 238, 238, 0.37);">
@endif
<?php $i++; ?>
@endforeach
</div>
</l
Upvotes: 0
Views: 4110
Reputation: 21681
I think you can try this :
<div class="form-group" id="group-form-id">
<label id="correct_answer" style="color: #00a65a;">
@lang('test-results.correct_answer'):
<div style="line-height: 8px; padding-top: 5px;">
<?php $i=1; ?>
@foreach($question->answers->sortBy('order') as $answer)
@if($answer->order)
<span id="test_span"><br><em style="padding-right: 10px; color: #999;">{{ $i }}.</em>{!! $answer->text !!}</span>
<hr style="width: 500%;border-top: 1px solid rgba(238, 238, 238, 0.37);">
@endif
<?php $i++; ?>
@endforeach
</div>
Hope this work for you !!!
Upvotes: 2
Reputation: 60
In the controller when you return the questions you can use query builders orderBy(order). See this documentation: Laravel Query Builder - Order By
Upvotes: 1
Reputation: 2333
You can sort the answers collection:
$question->answers->sortBy('order')
Check Laravel Collections
Upvotes: 0