创建一些像ol(html),但取决于搜索结果
问题描述:
我真的搞不清楚什么是最好的标题,但这是我想要实现的。创建一些像ol(html),但取决于搜索结果
我想要做的就是让第一列像html中的'ol'那样对搜索结果进行编号。
我有这个在我的刀锋:
<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>
这是我的控制器
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);
}
所以插入一个新的次代码之后,我应该在我的控制器做。
这是使用Laravel 5.2的方式
答
刀片模板:
<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>
而在你的控制器:
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);
}
答
你努力实现自己的目标的方式是对位'最佳实践'。最好的办法是将责任更好地封装起来。 (HTML生成属于查看和逻辑控制器)
所以从你的控制器中删除所有的HTML,并在您的视图做代(迭代)的一部分......它的存在,你想遍历并生成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>
我没有测试的代码,我刚才编辑给你的想法。
谢谢!非常简单:) –
这完全忽略了刀片的全部重点。你不应该把视图特定的逻辑放在控制器中。 Blade有一个@foreach关键字,您可以像VikingCode的答案中指定的那样使用它。 –
@RodneyZanoria @RodneyZanoria你的问题是“我应该在我的控制器中做什么”而不是“我能做什么”..你可以做什么Erol提出的,但你应该做的是写在我的回答 – VikingCode