PHP code example of gustocoder / laravel-datatable

1. Go to this page and download the library: Download gustocoder/laravel-datatable 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/ */

    

gustocoder / laravel-datatable example snippets

 
    namespace GustoCoder\LaravelDatatable\Http\Controllers;

    use App\Http\Controllers\Controller;
    use Gustocoder\LaravelDatatable\Http\Controllers\DatatableController;


    class ExampleController extends Controller
    {
        public function showUsers() {
            $dataTableClass = new DatatableController(
                'User', 
                'users', 
                [], 
                ['date_field' => 'created_at', 'orderBy' => 'created_at']
            );

            //give the full route path (NOT the route name) as defined in the route file, eg 'admin/users'
            $deleteRoute = 'deleteUser'; 
            $editRoute = 'editUsers'; 


            //This is optional, & it creates a new column, eg 'Actions'. It accepts the name of the column 
            //and you can call it whatever you want. It only supports adding one column, so only call this 
            //once. You can however, add multiple field buttons under that columns, and the column will be 
            //expanded to contain them all eg Actions.
            //If you do call addColumn, make sure you also call addFieldButton(...) to insert data under 
            //the new column
            $dataTableClass->addColumn('Action');

            //used to add field data to go under the column you added above. Handy for Edit, or Delete buttons.
            $dataTableClass->addFieldButton(
                'Action', 
                'Delete', 
                'x', 
                $deleteRoute, 
                ['id'], 
                ['id' => 'deleteUserBtn', 'class' => 'btn btn-danger btn-sm']
            );

            $dataTableClass->addFieldButton(
                'Action', 
                'Edit', 
                'Edit', 
                $editRoute, 
                ['id'], 
                ['id' => 'editUserBtn', 'class' => 'btn btn-warning btn-sm']
            );

            //add another button if you want. Give it relevant attributes
            $dataTableClass->addFieldButton(
                'Action', 
                'Something', 
                'something', 
                'someRoute', 
                ['id'], 
                ['id' => 'doSomethingBtn', 'class' => 'btn btn-primary btn-sm']
            );

            $panelId = 'usersPanel';
            $usersTable = $dataTableClass->getTable($panelId);
            return view('laravel-datatable::datatable-view', ['usersTable' => $usersTable]);
        }


        /**
         * An example of how you would delete a record from the datatable-see the delete
         * button link and the route that points to this method
        */
        public function deleteUser($userId)
        {
            $userModel = new User();
            $record = $userModel::find($userId);

            if ($record) {
                $record->delete();
                return redirect()->back()->with('success', 'User deleted successfully');
            }
            else 
            {
                return redirect()->back()->with('danger', 'User could not be deleted');
            }
        }
    }
 
    {!! $usersTable !!}

        ['date_field' => 'created_at', 'orderBy' => 'created_at']
    
 
            ['date_field' => 'created_at']
        
 
            ['orderBy' => 'created_at'] 
        

            ['heading' => 'Users data']
        

        use Illuminate\Support\Facades\Route;
        use Gustocoder\LaravelDatatable\Http\Controllers\ExampleController;

        //This is an example of how you would define routes for the feature
        Route::get('/users', [ExampleController::class, 'showUsers'])->name('show-users');
        Route::get(
            '/deleteUser/{userId}', 
            [ExampleController::class, 'deleteUser']
        )->name('delete-user');
    

        ...
        $dataTableClass = new DatatableController(...);
        ...
        $deleteRoute = 'deleteUser';
        ...
        $dataTableClass->addFieldButton(
            'Action', 
            'Delete', 
            'x', 
            $deleteRoute, 
            ['id'], 
            ['id' => 'deleteUserBtn', 'class' => 'btn btn-danger btn-sm']);
    
 
    $dataTableClass = new DatatableController('Blog', 'blog-comments');
 
    ...
    $dataTableClass->setJoinData(
            ["'blog_comments', 'blog.id', '=', 'blog_comments.blog_id'"],
            [
                'blog.blog_id as post_id', 
                'blog.blog_title', 
                'blog.blog_article', 
                'blog.blog_author as author', 
                'blog_comments.blog_comments_comment as comment', 
                'blog_comments.blog_comments_id as primary_key', 
                'blog_comments.blog_comments_author as commentor'
            ],
            ["'blog_comments.blog_comments_status', 'valid'"]
        );
 
    
    
    namespace App\Http\Controllers;

    use Gustocoder\LaravelDatatable\Http\Controllers\DatatableController;
    ...


    class AdminController extends Controller
    {
        public function blogComments()
        {
            $dataTableClass = new DatatableController('Blog', 'blog-comments');
            
            $dataTableClass->setJoinData(
                ["'blog_comments', 'blog.blog_id', '=', 'blog_comments_blog_id'"],
                [
                    'blog.blog_id as post_id', 
                    'blog.blog_title', 
                    'blog.blog_article', 
                    'blog.blog_author as author', 
                    'blog_comments.blog_comments_comment as comment',
                    //Specify the joined record's primary key field as 'primary_key' 
                    'blog_comments.blog_comments_id as primary_key', 
                    'blog_comments.blog_comments_author as commentor'
                ],
                ["'blog_comments.blog_comments_status', 'valid'"]
            );

            //-------------------Add columns & buttons (optional)-----------------------
            $deleteRoute = 'admin/blog-comments'; 
            $editRoute = 'admin/edit-blog-comments';
            $doSomethingRoute = 'admin/do-something-with-comments';

            $dataTableClass->addColumn('Stuff');

            //used to add field data to go under the column you added above. use this 
            //for Edit, or Delete buttons.
            $dataTableClass->addFieldButton(
                'Stuff', 
                'Delete', 
                'x', 
                $deleteRoute, 
                ['id'], 
                ['id' => 'deleteBlogCommentBtn', 'class' => 'btn btn-danger btn-sm']
            );

            $dataTableClass->addFieldButton(
                'Stuff', 
                'Edit', 
                'Edit', 
                $editRoute, 
                ['id'], 
                ['id' => 'editBlogCommentBtn', 'class' => 'btn btn-warning btn-sm']
            );

            $dataTableClass->addFieldButton(
                'Stuff', 
                'Sometype', 
                'Something', 
                $doSomethingRoute, 
                ['id'], 
                ['id' => 'doSomethingBtn', 'class' => 'btn btn-primary btn-sm']
            );
            //-------------------Add columns & buttons (optional)-------------------------

            $panelId = 'blogPanel'; 
            $blogCommentsTable = $dataTableClass->getTable($panelId);
            return view(
                'admin.contactMessages', 
                ['contactMessagesTable' => $blogCommentsTable]
            );
        }
    }

    'blog_panelId'          => '',
    'blog_heading'          => 'Blog',
    'blog_comments_heading' => 'Blog comments',
    'blog_date_field'       => 'blog_created',
    'blog_orderBy'          => 'blog_created',
    'blog_comments_orderBy' => 'blog_comments_created',

        'recordsPerpage'    => 5,
        'sortable'          => true,
        'sortOrder'         => 'ASC',
        'clickableRecs'     => true,

    return [
        ...
        Gustocoder\laravelDatatable\LaravelDatatableServiceProvider::class
    ];
bash
        php artisan vendor:publish