I couldn't really figure out what's the best title but here's what I want to achieve.
What I want to do is to make the first column be like an 'ol' in html where it numbers the search results.
I have this in my blade:
<table class="table table-bordered" style="text-align: left;">
<tr>
<th>Full Name (Last, First, Middle)</th>
<th>Encoded by</th>
</tr>
<tbody>
{!! $searchresults !!}
</tbody>
</table>
And this is my controller
public function listofStaffTable(){
$staffs = DB::table('staff_profiles')->select('last_name','first_name','middle_name','encoded_by')->orderBy('last_name','ASC')->get();
$searchresults = ""; // define the searchresults variable
foreach( $staffs as $key => $profile ){
$searchresults .= '<tr>'.
'<td><a href="'. URL::route('viewstaffprofile', $profile->id) .'">'. $profile->last_name .' '. $profile->first_name .' '. $profile->middle_name .' </a></td>'.
'<td>'. $profile->encoded_by .'</td>'.
</tr>';
}
$data = [];
$data['searchresults'] = $searchresults;
return view( 'user/masterlist/list-of-staffs', $data);
}
So after inserting a new th code, what should I do in my controller.
This is using Laravel 5.2 by the way
Blade Template :
<table class="table table-bordered" style="text-align: left;">
<tr>
<th>Order</th>
<th>Full Name (Last, First, Middle)</th>
<th>Encoded by</th>
</tr>
<tbody>
{!! $searchresults !!}
</tbody>
</table>
And in your Controller :
public function listofStaffTable()
{
$staffs = DB::table('staff_profiles')->select('last_name','first_name','middle_name','encoded_by')->orderBy('last_name','ASC')->get();
$searchresults = ""; // define the searchresults variable
$i = 1; // just use a integer that increments when iterating over results
foreach( $staffs as $key => $profile )
{
$searchresults .= '<tr>'.
'<td>'.$i.'.</td>'.
'<td><a href="'. URL::route('viewstaffprofile', $profile->id) .'">'. $profile->last_name .' '. $profile->first_name .' '. $profile->middle_name .' </a></td>'.
'<td>'. $profile->encoded_by .'</td>'.
'</tr>';
$i++;
}
$data = [];
$data['searchresults'] = $searchresults;
return view( 'user/masterlist/list-of-staffs', $data);
}
The way you try to achieve your goal is a bit against the 'best practice'. Best approach would be to encapsulate the responsibilities a bit better. (html generation belongs to view and logic in your controller)
So remove all html from your controller and do the generation (iteration) part in your view ... it's there that you suppose to iterate and generate your html
public function listofStaffTable()
{
$staffs = DB::table('staff_profiles')
->select('last_name','first_name','middle_name','encoded_by')
->orderBy('last_name','ASC')
->get();
$data = [];
$data['searchresults'] = $searchresults;
return view( 'user/masterlist/list-of-staffs', $data);
}
<table class="table table-bordered" style="text-align: left;">
<tr>
<th>Order</th>
<th>Full Name (Last, First, Middle)</th>
<th>Encoded by</th>
</tr>
<tbody>
@foreach ($searchresults as $key => $profile )
<tr>
<!-- The current loop iteration (starts at 1). -->
<td> {{ $loop->iteration }} </td>
<td>
<a href="{{ route('viewstaffprofile', $profile->id) }} ">
{{ $profile->last_name }} {{ $profile->first_name }} {{ $profile->middle_name }}
</a>
</td>
<td> {{ $profile->encoded_by }} </td>
</tr>
@endforeach
</tbody>
</table>
I did not test the code, i just edited to give you an idea.