dimanche 18 avril 2021

How to fetch huge data in datatable using laravel?

views:

<script type="text/javascript">
    $(document).ready(function(){
        $.ajaxSetup ({
            headers: {
                'X-CSRF-TOKEN': $ ('meta[name="csrf-token"]').attr ('content')
            }
        });

        var postsTable = $ ('#example1').dataTable ({
            "ordering": true,
            "processing": true,
            "serverSide": true,
            "ajax": {
                "url": "",
                "dataType": "json",
                "type": "POST"
            },
            "columns": [
                { "data": "sn" },
                { "data": "name" },
                { "data": "username" },
                { "data": "mobile_number" },
                { "data": "designation" },
                { "data": "create_at" },
                { "data": "action" }
            ],
            stateSave: false,
        });

        $ ('.data-table-search-input-text').on ('keyup change', function () {
            var inputElement = $ (this);

            ojoDelay (function () {

                var i = inputElement.attr ('data-column');
                var v = inputElement.val ();
                postsTable.api ().columns (i).search (v).draw ();
                //usersTable.api ().ajax.reload ();
            }, 1000, this);
        });
    });  
</script>
<script>
    ojoDelay = (function () {
        var timer = 0;
        return function (callback, ms, that) {
            clearTimeout (timer);
            timer = setTimeout (callback.bind (that), ms);
        };
    }) ();
</script>

route:

Route::post('/admin/user/search', ['as' => 'admin.user.search', 'uses' => 'admin\UserController@search', 'middleware' => 'AuthPortalKas:'.serialize(['ADMIN']), 'allow' => ['ADMIN']]);

Controllers:

public function search(Request $request) 
{
    $posts = DB::table('users')
            ->select('*')
            ->leftjoin('roles', 'users.user_id', '=', 'roles.user_id')
            ->where('users.status', '!=', 'Delete')
            ->where('roles.role_id', '!=', 1)
            ->get();
    $totalData = count($posts);
    $totalFiltered = $totalData;
    $data = array();
    if(!empty($posts))
    {
        $i = 1;
        foreach ($posts as $post)
        {
            $nestedData['sn'] = $i;
            $nestedData['name'] = $post->name;
            $nestedData['username'] = $post->first_name." ".$post->last_name;
            $nestedData['mobile_number'] = $post->mobile_number;
            $nestedData['designation'] = $post->designation;
            $nestedData['create_at'] = $post->created_at;

            $buttons = '<a class="btn btn-success" href=""><i class="fa fa-search-plus"></i></a>
            <a class="btn btn-info" href=""><i class="fa fa-edit"></i></a><a class="btn btn-danger" onclick="delete_record(, )"><i class="fa fa-trash-o"></i></a>';

            $nestedData['action'] = $buttons;
            $data[] = $nestedData;
            $i++;
        }
    }

    $json_data = array(
                    "draw"            => intval($request->get('draw')),  
                    "recordsTotal"    => $totalData,  
                    "recordsFiltered" => $totalFiltered, 
                    "data"            => $data   
                );

    echo json_encode($json_data);
}

In the above question I have approx 1 million data where I want to fetch and show data in datatable but what happen here when I simply run http://localhost/project/admin/user/search then it show error. I was thinking that I have problem with my route file but I don't have any idea where am I doing wrong? Please help me to solve this.

Thank You



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3uZjbgi
via IFTTT

Aucun commentaire:

Enregistrer un commentaire