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/ */
$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);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.