问题
I am trying to build a dataTable with custom filtering with the help of yajra datatable from here :
HTML table in view :
<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
As to common.datatables
, datatables.blade.php
has :
$.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 value $routeValue refers to URL_STUDENT_EXAM_GETATTEMPTS
constant (to be clarified later) used in view in whatever way.
When I select a value from the "batch"
drop-down and press the submit
button, an ajax call is made to the route. In browser inspection tool, I see that a lot of query parameters are added in the ajax URL and our batch
param is also there. Now I need to retrieve that batch
parameter inside the controller.
Now about the server side code :
The constant URL_STUDENT_EXAM_GETATTEMPTS
used in blade has the value PREFIX.'exams/student/get-exam-attempts/'
And in route.php
, the route is defined as :
Route::get('exams/student/get-exam-attempts/{user_slug}/{exam_slug?}', 'StudentQuizController@getExamAttemptsData');
In controller I have :
public function getExamAttemptsData(Request $request,$slug, $exam_slug = '')
{
//body goes here
}
I have used all the following methods to get the batch
parameter in the controller but in vain :
$request->get('batch')
$request->query('batch')
Input::get('batch')
How can I retrieve the value of batch
inside the controller ?
EDIT: BTW I am using use Illuminate\Http\Request;
for the Request $request
variable in controller function parameter
EDIT2: The browser inspection tool shows the ajax request url as :
http:// localhost/lcbs/exams/student/get-exam-attempts/myuser123 ?draw=2&columns%5B0%5D%5Bdata%5D=0......search%5Bregex%5D=false&batch=22&_=1541684388689.
So you see that batch
is there in the query parameters.
But inside the controller, the code $request->getQueryString()
only shows
%2Fexams%2Fstudent%2Fget-exam-attempts%2Fmyuser123
And \URL::current()
shows http:// localhost/lcbs/exams/student/get-exam-attempts/myuser123
That means, the $request
misses the complete query string.
EDIT3: @ Adrian Hernandez-Lopez, I am pasting the FULL content of Kernel.php here :
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,
];
}
回答1:
Looks like the ? in the Route is not used to define an optional parameter but a string instead:
Route::get('exams/student/get-exam-attempts/{user_slug}/{exam_slug?}', 'StudentQuizController@getExamAttemptsData');
Could you please change it and see what you get?
Route::get('exams/student/get-exam-attempts/{user_slug}/{exam_slug}', 'StudentQuizController@getExamAttemptsData');
Also, the query string you posted has a space after myyser123
, this could also explain the issue.
http:// localhost/lcbs/exams/student/get-exam-attempts/myuser123 ?draw=2&columns%5B0%5D%5Bdata%5D=0......search%5Bregex%5D=false&batch=22&_=1541684388689.
So I would suggest to check how the value is defined in the javascript code
url: '{{ $routeValue }}',
来源:https://stackoverflow.com/questions/53208578/laravel-yajra-datatable-cannot-retrieve-the-search-parameter-from-ajax-call-in