PHP code example of ymigval / laravel-model-datatable-ssp

1. Go to this page and download the library: Download ymigval/laravel-model-datatable-ssp library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ymigval / laravel-model-datatable-ssp example snippets


use App\Models\Customer;

return (new Customer())->datatable([
    'first_name', 'last_name', 'phone'
]);

use App\Models\Customer;

return Customer::datatable([
    'first_name', 'last_name', 'phone'
]);

use App\Models.Customer;

return Customer::where('type', 'male')->datatable([
    'first_name',
    'last_name',
    'active' => function ($field, $row) {
        return ($field) ? 'Yes' : 'No';
    }
]);

use App\Models\Customer;

return Customer::datatable([
    'first_name',
    'last_name',
    function () {
        return 'additional column #1';
    },
    function () {
        return 'additional column #2';
    }
]);

use App\Models\Customer;

return Customer::datatable([
    'first_name' => function ($field, $row) {
        return $field . ' ' . $row->last_name;
    }
], ['last_name']);

use App\Models\Customer;

return Customer::datatable([
    'first_name' => function ($field, $row) {
        return $field . ' ' . $row->last_name;
    }
], ['last_name' => ['orderable' => true, 'searchable' => true]]);

use Illuminate\Support\Facades\DB;

return DB::table('customers')
    ->datatable([
        'first_name',
        'last_name',
        'phone'
    ]);

use App\Models\Customer;

(new Customer())->datatable(
    ['first_name', 'last_name', 'phone'],
    [],
    'array'
);

use Illuminate\Support\Facades\DB;

DB::table('customers')->datatable(
    ['first_name', 'last_name', 'phone'],
    [],
    'json'
);

use App\Models\Customer;

return (new Customer())->datatable(
    function () {
        return ['first_name', 'last_name', 'phone'];
    }
);

use App\Models\Customer;

return Customer::join('business', 'business.id_customer', '=', 'customers.id')
    ->datatable(
        function () {
            return ['customers.first_name', 'customers.last_name', 'business.name'];
        }
    );

use App\Models\Customer;

return Customer::join('business', 'business.id_customer', '=', 'customers.id')
    ->datatable(
        [
            'customers.first_name AS f_name',
            'customers.last_name AS l_name',
            'business.name AS aaa',
        ],
        ['customers.phone AS contact' => ['orderable' => false, 'searchable' => true]]
    );

use App\Models\Customer;

return Customer::with('business')
    ->datatable(
        [
            'first_name',
            'last_name',
            function ($field, $row) {
                return $row->business->name;
            },
        ],
        ['id'] // 'id' is the localKey field specified in the relation with 'business'
    );