Donations Appreciated

The simplest acts of kindness are by far more powerful then a thousand heads bowing in prayer. - Mahatma Gandhi

Click here to lend your support to: Laravel Datatables and make a donation at pledgie.com !

CollectionController.php - Collection Demo using Github API as data source

Stars Repo Owner Description Private

CollectionController.php

    public function getGithub()
    {
        return view('datatables.collection.github');
    }

    public function getGithubData()
    {
        $search = $request->get('search');
        $keyword = $search['value']?: 'laravel';
        $repositories = Cache::get($keyword, function() use($keyword) {
            $client = new \GuzzleHttp\Client();
            $response = $client->get('https://api.github.com/search/repositories', [
                    'query' => ['q' => $keyword]
                ]);
            $repositories = $response->json();
            Cache::put($keyword, $repositories, 10);

            return $repositories;
        });

        $data = new Collection($repositories['items']);

        return Datatables::of($data)
            ->editColumn('stargazers_count', function($row) {
                return '<div class="input-group input-group-sm">
                            <span class="input-group-addon"><i class="glyphicon glyphicon-star"></i></span>
                            <input type="text" class="form-control" style="width:64px" readonly
                                value="'. number_format($row['stargazers_count'] , 0) .'">
                        </div>';
            })
            ->editColumn('full_name', function($row) {
                return HTML::link($row['html_url'], $row['full_name']);
            })
            ->editColumn('private', function($row) {
                return $row['private'] ? 'Y' : 'N';
            })
            ->filter(function(){}) // disable built-in search function
            ->make(true);
    }

Javascript

    $('#datatable').dataTable({
        processing: true,
        serverSide: true,
        ajax: 'https://datatables.yajrabox.com/collection/github-data',
        columns: [
            {data: 'stargazers_count', name: 'stargazers_count'},
            {data: 'full_name', name: 'full_name'},
            {data: 'owner.login', name: 'owner.login', orderable: false, searchable: false},
            {data: 'description', name: 'description'},
            {data: 'private', name: 'private'}
        ],
        order: [[0, 'desc']],
        displayLength: -1
    }).fnFilterOnReturn();