PHP code example of bwebi / datatables

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

    

bwebi / datatables example snippets


$ php artisan config:publish bllim/datatables

$todo = ToDoList::select(array('todo.id','todo.name','todo.created_at','todo.status'));

return Datatables::of($todo)
    ->set_index_column('id')
    ->set_row_class('@if($status=="done") success @endif')
    ->set_row_data('created_at','{{$created_at}}')
    ->make();

$users = Models\User::select()->ModelJoin('profile');
        return $dataTables = Datatables::of($users)
            ->filter_column('profile.last_name','where',\DB::raw('CONCAT(profile.last_name,\' \',profile.first_name)'),'LIKE','$1')
            ->filter_column('created_at','where','users.created_at','LIKE','$1')
            //for the blade template only the array data results is provided, it is `extracted` into the template
            ->edit_column('profile.last_name', '{{ $profile["first_name"]." ".$profile["last_name"] }}')
            ->edit_column('created_at', function($result_obj) {
                //in a callback, the Eloquent object is returned so carbon may be used
                return $result_obj->created_at->format('d/m/Y - h:ia');
            })
            ->add_column('manage', '<a href="/user/edit/{{$id}}" >Edit</a>', 3)
            ->remove_column('profile.photo_id')
            ->set_index_column('row-{{ $id }}')
            ->make();

//helper scope method in base Model class
    public function scopeModelJoin($query, $relation_name, $operator = '=', $type = 'left', $where = false) {
        $relation = $this->$relation_name();
        $table = $relation->getRelated()->getTable();
        $one = $relation->getQualifiedParentKeyName();
        $two = $relation->getForeignKey();

        if (empty($query->columns)) {
            $query->select($this->getTable().".*");
        }

        //$join_alias = $table;
        $prefix = $query->getQuery()->getGrammar()->getTablePrefix();
        $join_alias = $relation_name;
        foreach (\Schema::getColumnListing($table) as $related_column) {
            $query->addSelect(\DB::raw("`$prefix$join_alias`.`$related_column` AS `$join_alias.$related_column`"));
        }
        $two = str_replace($table . ".", $join_alias . ".", $two);
        return $query->join("$table AS $prefix$relation_name", $one, $operator, $two, $type, $where); //->with($relation_name);
    }