Donations Appreciated

When we give cheerfully and accept gratefully, everyone is blessed. - Maya Angelou

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

CollectionController.php - Collection Demo using collection of object as data source


Heads Up! Data used here are random using faker. Filtering/sorting results data will be on a luck basis

Id Name Email Created At Updated At

CollectionController.php

    public function getObject()
    {
        return view('datatables.collection.object');
    }

    public function getObjectData()
    {
        $faker = Faker::create();
        $data  = [];
        for ($i = 0; $i < 100; $i++) {
            $obj = new \stdClass;
            $obj->id = $i + 1;
            $obj->name = $faker->name;
            $obj->email = $faker->email;
            $obj->created_at = Carbon::now();
            $obj->updated_at = Carbon::now();
            $data[] = $obj;
        }
        $users = new Collection($data);

        return Datatables::of($users)->make(true);
    }

Javascript

    $('#users-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: 'https://datatables.yajrabox.com/collection/object-data',
        columns: [
            {data: 'id', name: 'id'},
            {data: 'name', name: 'name'},
            {data: 'email', name: 'email'},
            {data: 'created_at.date', name: 'created_at'},
            {data: 'updated_at.date', name: 'updated_at'}
        ]
    });