Laravel Datatables with custom date range filter server-side processing
<p>I have a datatables in my system which I want to optimize. I have Laravel and jQuery Datatables. My problem is I want to optimize the rendering of data if my data grid needs to have custom filter before drawing any data. In my other Datatables I have data grid which will pull data on page load which is fine and I can do server-side processing by just following this guide:</p>

<p><a href="http://datatables.yajrabox.com/eloquent/basic" rel="nofollow">http://datatables.yajrabox.com/eloquent/basic</a></p>

<p>However what if I have 3 filters like <code>From Date, To Date, Agent Name</code> I can make it work but my concern is my code is writing the data client side upon getting ajax response which will take time if I have very huge data set. My current code is:</p>

<pre><code>$("#FormListBtn").click(function() {
   var table = $('#FormList').DataTable();
    $.ajax({
    url: "form-list",
    type: 'GET',
    data: {"from" : $("#datePickerFrom").val(), "to" :  $("#datePickerTo").val(), "agent_id" :  $("#agent_id").val()},
    success: function(result){
    var myObj = $.parseJSON(result);
            $.each(myObj, function(key,value) {
            var t = $('#FormList').DataTable();
            t.row.add( [
                value.id,
                value.calldatetime,
                value.survey,
                value.agent,
                value.campaign,
                value.name,
                value.phone,
                value.average_score,
                "&lt;a class='btn btn-small btn-info' href='&lt;?php echo URL::to('form').'/';?&gt;"+value.id+"/edit'&gt;&lt;span class='glyphicon glyphicon glyphicon-edit' aria-hidden='true'&gt;&lt;/span&gt;&lt;/a&gt;",
                "&lt;form method='POST' action='&lt;?php echo URL::to('form').'/';?&gt;"+value.id+"' accept-charset='UTF-8' class='pull-left' &gt;"+
                "&lt;input name='_method' type='hidden' value='DELETE'&gt;"+
                "&lt;button type='submit' class='btn btn-warning'&gt;&lt;span class='glyphicon glyphicon-trash' aria-hidden='true'&gt;&lt;/span&gt;&lt;/button&gt;"+"&lt;/form&gt;",

            ] ).draw();
        });
    }}); 
});
</code></pre>

<p>As you can see I am writting the rows on a loop which will take time in very huge data. Is there another way to make it faster?</p>

<p>The process is: User will select date range and agent name for the filter then clicks submit then generate the table.</p>

<p>My Controller </p>

<pre><code>public function getFormList()
{
    $from = Input::get("from");
    $to   = Input::get("to");
    $agent_id   = Input::get("agent_id");

    if($agent_id != '')
    {
        $data2 = DB::table('forms')
        -&gt;join('agents', 'agents.id', '=', 'forms.agent_id')
        -&gt;join('campaigns', 'campaigns.id', '=', 'forms.campaign_id')
        -&gt;join('surveys', 'surveys.id', '=', 'forms.survey_id')
        -&gt;join('users', 'users.id', '=', 'forms.evaluatedby_id')
        -&gt;select(
            'forms.id', 
            'forms.calldatetime', 
            'forms.phone', 
            'forms.calldatetime', 
            'forms.average_score', 
            'forms.auto_fail', 
            'forms.yes_count', 
            'forms.no_count', 
            'forms.na_count', 
            'users.name', 
            'agents.name as agent', 
            'campaigns.name as campaign',
            'surveys.name as survey'
            )
        -&gt;where('calldatetime', '&gt;=', $from)
        -&gt;where('calldatetime', '&lt;=', $to)
        -&gt;where('forms.agent_id', '=', $agent_id)
        -&gt;get();
        return json_encode($data2);
    }
    else
    {
        $data = DB::table('forms')
        -&gt;join('agents', 'agents.id', '=', 'forms.agent_id')
        -&gt;join('campaigns', 'campaigns.id', '=', 'forms.campaign_id')
        -&gt;join('surveys', 'surveys.id', '=', 'forms.survey_id')
        -&gt;join('users', 'users.id', '=', 'forms.evaluatedby_id')
        -&gt;select(
            'forms.id', 
            'forms.calldatetime', 
            'forms.phone', 
            'forms.calldatetime', 
            'forms.average_score', 
            'forms.auto_fail', 
            'forms.yes_count', 
            'forms.no_count', 
            'forms.na_count', 
            'users.name', 
            'agents.name as agent', 
            'campaigns.name as campaign',
            'surveys.name as survey'
            )
        -&gt;where('calldatetime', '&gt;=', $from)
        -&gt;where('calldatetime', '&lt;=', $to)
        -&gt;get();
        return json_encode($data);
    }

}
</code></pre>