Downloads
Stars
Version
Package of Support for development on Laravel
composer require laravelha/support
Tableable
configures models represented in the data table.
use Laravelha\Support\Traits\Tableable;
class Modelo extends Model {
//
}
class Modelo extends Model {
use Tableable;
//
}
getColumns
method. /**
* ['data' => 'columnName', 'searchable' => true, 'orderable' => true, 'linkable' => false]
*
* searchable and orderable is true by default
* linkable is false by default
*
* @return array[]
*/
public static function getColumns(): array
{
return [
['data' => 'id', 'linkable' => true],
['data' => 'title'],
['data' => 'subtitle'],
['data' => 'slug'],
['data' => 'content'],
];
}
The options 'searchable' and 'orderable' are 'true' by default, but 'linkable' is 'false', if not informed.
public function index()
{
$columns = Model::getColumns();
return view('models.index', compact('columns'));
}
public function data()
{
return Model::getDatatable();
}
Route::get('/models/data', 'ModelController@data')->name('models.data');
<table id="models-table">
<thead>
<tr>
@foreach($columns as $column)
<th>@lang('models.'.$column['data'])</th>
@endforeach
</tr>
</thead>
</table>
@push('scripts')
<script id="script">
$(function () {
var table = $('#models-table').DataTable({
serverSide: true,
processing: true,
responsive: true,
order: [ [0, 'desc'] ],
ajax: {
url: 'models/data'
},
columns: @json($columns),
pagingType: 'full_numbers'
});
});
</script>
@endpush
RequestQueryBuildable
makes behavior of SQL queries by parameters in the request.
?only
separated by commas to filter columns like 'select' in SQLquery: only=field1;field2;field3...
?search
with key and value to apply where or whereHasquery: ?search=key:value,key2:value2
class MyModel extends Model {
public static function searchable() {
return [
'key' => 'operator',
'key2' => 'operator',
];
}
// ...model
}
define searchable method on model is needed and relationships are identified by dot relation.column
?operators
to change search operator dynamicallyquery: field1:operator1
?sort
to sort resultsquery: ?sort=field:direction;field2:direction
?with
to load relation data separated by commasquery: relation1;relation2;relation3...