我正在尝试在 yajra 数据表的帮助下从这里构建一个带有自定义过滤的数据表:
视图中的 HTML 表:
<form id="search-form" class="form-inline" method="POST" role="form" action="#">
<select id="batch" name="batch">
<option value="">Select</option>
<option value="22">Skill Level CBE Dec 2018</option>
<option value="2">Batch 2</option>
</select>
<button class="btn btn-primary" type="submit">Search</button>
</form>
<table class="table table-striped table-bordered datatable" cellspacing="0" width="100%">
<thead>
<tr>
<th>{{ getPhrase('S/N')}}</th>
<th>{{ getPhrase('Batch')}}</th>
<th>{{ getPhrase('Course')}}</th>
<th>{{ getPhrase('Subject')}}</th>
<th>{{ getPhrase('title')}}</th>
<th>{{ getPhrase('otq_marks')}}</th>
<th>{{ getPhrase('cr_marks')}}</th>
<th>{{ getPhrase('total')}}</th>
<th>{{ getPhrase('average')}}</th>
</tr>
</thead>
</table>
@section('footer_scripts')
@include('common.datatables', array('route'=>URL_STUDENT_EXAM_GETATTEMPTS.$user->slug, 'route_as_url' => 'TRUE'))
@stop
至于common.datatables
,datatables.blade.php
有:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
tableObj = $('.datatable').DataTable({
processing: true,
serverSide: true,
retrieve :true,
// cache: true,
type: 'GET',
ajax: {
url: '{{ $routeValue }}',
data: function (d) {
d.batch = $('#batch').val();
}
}
});
$('#search-form').on('submit', function(e) {
tableObj.draw();
e.preventDefault();
});
ajax url 值 $routeValue 是指以URL_STUDENT_EXAM_GETATTEMPTS
任何方式在视图中使用的常量(稍后会澄清)。
当我从"batch"
下拉列表中选择一个值并按下submit
按钮时,将对路由进行 ajax 调用。在浏览器检查工具中,我看到ajax URL中添加了很多查询参数,我们的batch
参数也在那里。现在我需要batch
在控制器中检索该参数。
现在关于服务器端代码:
URL_STUDENT_EXAM_GETATTEMPTS
刀片中使用的常数 具有值PREFIX.'exams/student/get-exam-attempts/'
在 中route.php
,路线定义为:
Route::get('exams/student/get-exam-attempts/{user_slug}/{exam_slug?}', 'StudentQuizController@getExamAttemptsData');
在控制器中我有:
public function getExamAttemptsData(Request $request,$slug, $exam_slug = '')
{
//body goes here
}
我使用了以下所有方法来获取batch
控制器中的参数,但徒劳无功:
$request->get('batch')
$request->query('batch')
Input::get('batch')
如何检索batch
控制器内部的值?
编辑:顺便说一句,我use Illuminate\Http\Request;
用于Request $request
控制器功能参数中的变量
EDIT2: 浏览器检查工具将 ajax 请求 url 显示为:
http://localhost/lcbs/exams/student/get-exam-attempts/myuser123 ?draw=2&columns%5B0%5D%5Bdata%5D=0......search%5Bregex%5D=false& batch=22 &_ =1541684388689。
batch
因此,您会在查询参数中看到它。但在控制器内部,代码$request->getQueryString()
只显示
%2Fexams%2Fstudent%2Fget-exam-attempts%2Fmyuser123
并\URL::current()
显示 http://localhost/lcbs/exams/student/get-exam-attempts/myuser123
这意味着,$request
错过了完整的查询字符串。
EDIT3: @ Adrian Hernandez-Lopez,我在这里粘贴 Kernel.php 的全部内容:
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
'api' => [
'throttle:60,1',
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'role' => \Zizaco\Entrust\Middleware\EntrustRole::class,
'permission' => \Zizaco\Entrust\Middleware\EntrustPermission::class,
'ability' => \Zizaco\Entrust\Middleware\EntrustAbility::class,
// 'adminOrGuest' => \App\Http\Middleware\AdminOrGuestMiddleware::class,
];
}