laravelha / support
Laravelha Support
Installs: 4 032
Dependents: 4
Suggesters: 0
Security: 0
Stars: 8
Watchers: 3
Forks: 2
Open Issues: 0
Requires
- illuminate/support: ^6.0|^7.0|^8.0
README
Package of Support for development on Laravel
Install
composer require laravelha/support
Features
- Model Traits
- Tableable
- RequestQueryBuildable
How Tableable works
Tableable
configures models represented in the data table.
Requirements
Step-by-step
- Import the trait on model.
use Laravelha\Support\Traits\Tableable; class Modelo extends Model { // }
- Add the trait within class.
class Modelo extends Model { use Tableable; // }
- Implement the
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.
- On index action get the columns configuration.
public function index() { $columns = Model::getColumns(); return view('models.index', compact('columns')); }
- Create data action on controller.
public function data() { return Model::getDatatable(); }
- Create route for data action.
Route::get('/models/data', 'ModelController@data')->name('models.data');
- Create table on index blade.
<table id="models-table"> <thead> <tr> @foreach($columns as $column) <th>@lang('models.'.$column['data'])</th> @endforeach </tr> </thead> </table>
- Config script.
@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
How RequestQueryBuildable works?
RequestQueryBuildable
makes behavior of SQL queries by parameters in the request.
- Use
?only
separated by commas to filter columns like 'select' in SQL
query: only=field1;field2;field3...
- Use
?search
with key and value to apply where or whereHas
query: ?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
- Use
?operators
to change search operator dynamically
query: field1:operator1
- Use
?sort
to sort results
query: ?sort=field:direction;field2:direction
- Use
?with
to load relation data separated by commas
query: relation1;relation2;relation3...