Version: 2.0.0
A package for managing table data. convert your model or list of your data to table object, easily...
install package to your laravel project:
composer require irpcpro/table-soft
Register the TableSoft service provider by adding it to the providers in config/app.php
file.
'providers' => [
...
...
\Irpcpro\TableSoft\ServiceProviders\TableSoftServiceProvider::class,
]
If you want you can alias the TableSoft facade by adding it to the aliases in config/app.php
file.
'aliases' => Facade::defaultAliases()->merge([
...
...
'TableSoft' => \Irpcpro\TableSoft\Facade\TableSoftFacade::class,
])->toArray(),
- Pass Collection or Builder data into the facade.
use TableSoft;
class HomeController extends Controller {
public function index(){
// get data from collection
$data = collect([ [..], [..], [..] ]);
// Or ..
// get data from models
$data = App\Models\Product::query();
// finally pass the data to TableSoft
$table = TableSoft::data($data);
}
}
for adding column to table:
$table->column('Title') // default key name = Title
$table->column('Title', 'columnTitle')
$table->column('Title', 'columnTitle:string') // default type column is string
can use these type of data:
- int
- string
- float
- date
- bool
$table->column('Title', 'columnTitle:string', 'sort') // default ASC
$table->column('Title', 'columnTitle:string', 'sort:asc')
can use these type of sorting data:
- asc
- desc
$table->column('Price', 'price:int', 'sort', function($value){
return $value . '$';
});
also use without sorting data
$table->column('Price', 'price:int', function($value){
return $value . '$';
});
the second (fieldName:type) parameter must be set
$table->column('Price', 'price:int', function($value){
return $value . '$';
})->searchable();
or set after define column:
$table = $table->column('Price', 'price:int', function($value){
return $value . '$';
});
$table->searchable();
$table->setWidth(20);
$table->setWidth(20, 'px');
set measure in second parameter
- px
- %
$table->rowCounter('row', 'row-name:string', function($val){
return $value;
});
$table->paginate(10);
if set 0 it will return all data. (without limitation)
$table->setCaching('id-name-table');
id-name-table
should be a specific and unique string for this table.
$data = Http::get('https://...../products');
$data = collect($data->json());
// get data
$data = Product::query();
// set table
$table = TableSoft::data($data);
$table = $table->column('Title', 'title:string', 'sort')->searchable();
$table = $table->column('Image', 'thumbnail:string', function($value){
return "<img src='$value'/>";
});
$table = $table->column('Description', 'description:string', 'sort:asc')->searchable();
$table = $table->column('Price', 'price:int', 'sort', function($value){
return $value . '$';
})->setWidth(50, 'px')->searchable();
$table = $table->rowCounter('row')->setWidth(20,'px');
$table = $table->setCaching('table-product4');
$table = $table->paginate(10);
// get table
$data = $table->get();
- the response have several controller for manage your table:
array:5 [▼
"head" => Illuminate\Support\Collection {#334 ▶}
"body" => Illuminate\Pagination\LengthAwarePaginator {#339 ▶}
"sort_fields" => Illuminate\Support\Collection {#316 ▶}
"query_params" => array:3 [▶]
"exists" => true
]
the data of head
and body
have same data structure:
{
+title: "Description"
+name: "description"
+type: "string"
+sort: "sort"
+sortBy: "asc"
+value: "Description"
+width: null
+widthMeasure: null
+searchable: true
}
<table class="table table-bordered">
<thead>
<tr>
@foreach($data['head'] as $head)
<th width="{{$head->width ? $head->width.$head->widthMeasure : ''}}">{{$head}}</th>
@endforeach
</tr>
</thead>
<tbody>
@foreach($data['body'] as $body)
<tr>
@foreach($body as $item)
<td width="{{$item->width ? $item->width.$item->widthMeasure : ''}}">{!! $item !!}</td>
@endforeach
</tr>
@endforeach
</tbody>
</table>