我在索引页面中有一个搜索选项,当我搜索一个名称时,它会过滤一些细节,当我点击数据表中的show icon时它会显示到表格中,它应该重定向到show.index页面,但它会抛出这样的错误。但是它正在索引页面上工作,而不是在索引页面中搜索
索引页
@include('theme.header')
<script>
$(document).ready(function () {
$('#search').on('click', function () {
$value = $(this).val();
$.ajax({
type: 'GET',
url: '{{\Illuminate\Support\Facades\URL::to('search')}}',
data: {'search': $value},
success: function (data) {
$('#edpinfo').html(data);
}
})
})
})
</script>
<script>
$.ajaxSetup({headers: {'csrftoken': '{{ csrf_token() }}'}});
</script>
<br>
<div class="row">
<div class="col-12">
<div class="card m-b-30">
<div class="card-body">
<h4 class="mt-0 header-title">Indents</h4>
<input type="text" id="search" class="pull-right form-rounded">
<a href="{{route('edp')}}">
<button class="btn btn-sm btn-primary pull-left">Back</button>
</a>
<label class="pull-right">search</label>
<br>
<br><br>
<table id="datatable" class="table table-bordered table-responsive-lg">
<thead>
<tr>
<th>Slno</th>
<th>Customer Name</th>
<th>Customer Phone Number</th>
<th>DateOfDelivery</th>
<th>Delivery At</th>
<th>Show</th>
</tr>
</thead>
<tbody id="edpinfo">
@foreach($indents as $indent)
<tr>
<td>{{$loop->iteration}}</td>
<td>{{$indent->customer_name}}</td>
<td>{{$indent->phone_no}}</td>
<td>{{$indent->date_of_delivery}}</td>
<td>{{$indent->delivery_at}}</td>
<td><a href="{{route('edp.show',$indent->id)}}"><img src="assets/images/select.jpg"
class="imgsize"></a></td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div> <!-- end col -->
</div> <!-- end row -->
@include('theme.footer')
带搜索功能的控制器
public function search(Request $request)
{
if ($request->ajax()) {
$output = "";
$indents = DB::table('indents')
->where('id', 'LIKE', '%' . $request->search . '%')
->orwhere('customer_name', 'LIKE', '%' . $request->search . '%')
->get();
foreach ($indents as $key => $indent) {
$output .= '<tr>' .
'<td>' . $indent->id . '</td>' .
'<td>' . $indent->customer_name . '</td>' .
'<td>' . $indent->phone_no . '</td>' .
'<td>' . $indent->date_of_delivery . '</td>' .
'<td>' . $indent->delivery_at . '</td>' .
'<td>'.'.<a href="{{route(\'edp.show\',$indent->id)}}">.'.'<img src="assets/images/select.jpg" class="imgsize">.'.'</a>.'.'</td>'.
'</tr>';
}
return Response($output);
}
}
路线档案
Route::get('edp', 'EdpController@index')->name('edp');
Route::get('edp/cancel/{id}', 'EdpController@cancel')->name('edp.cancel');
Route::get('search', 'EdpController@search')->name('search');
问题在于搜索功能路由的控制器
'<td>'.'.<a href="{{route(\'edp.show\',$indent->id)}}">.'.'<img src="assets/images/select.jpg" class="imgsize">.'.'</a>.'.'</td>'.
答案 0 :(得分:1)
试试这个:
'<td>'.'.<a href="'.route('edp.show',$indent->id).'">.'.'<img src="assets/images/select.jpg" class="imgsize">.'.'</a>.'.'</td>'.
这是因为你在为你的表生成html时在你的控制器中使用{{}},当你使用脚本将html设置为你的表时,你的脚本中的PHP代码永远不会通过ajax在你的脚本中处理因为页面已经加载。
答案 1 :(得分:0)
如果您检查锚标记中的网址是否正确。我想你需要改变
'<td>'.'.<a href="{{route(\'edp.show\',$indent->id)}}">.'.'<img src="assets/images/select.jpg" class="imgsize">.'.'</a>.'.'</td>'.
到
'<td>'.'.<a href="'.route('edp.show',$indent->id).'">.'.'<img src="assets/images/select.jpg" class="imgsize">.'.'</a>.'.'</td>'.
另外我认为没有名称路由edp.show,也应该是路由(&#39; edp&#39;,$ indent-&gt; id)
答案 2 :(得分:0)
用url替换href中的路由。
$join = DB::table('leaves_policy')
->join('leaves_policies', 'leaves_policy.leave_policy', '=', 'leaves_policies.title')
->join('leaves_requests', 'leaves_policy.requested_by' , '=', 'leaves_requests.requested_by')
->select('leaves_policy.*', 'leaves_policies.title', 'leaves_policies.total_no_of_leaves_allowed_per_year',
'leaves_policies.no_of_months_leaves_valid', 'leaves_policies.max_leaves_per_month', 'leaves_policies.max_con_leaves_per_month',
'leaves_requests.leave_status')
->groupBy('leaves_policy.id')
->get();
试试这个