laravelha/support

Laravelha Support

1.0.3 2020-04-22 00:26 UTC

This package is auto-updated.

Last update: 2020-08-22 02:26:14 UTC


README

Package of Support for development on Laravel

Install

composer require laravelha/support

Features

  1. Model Traits
    • Tableable
    • RequestQueryBuildable

How Tableable works

Tableable configures models represented in the data table.

Requirements

Step-by-step

  1. Import the trait on model.
use Laravelha\Support\Traits\Tableable;

class Modelo extends Model {
    //
}
  1. Add the trait within class.
class Modelo extends Model {
    use Tableable;
    //
}
  1. 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.

  1. On index action get the columns configuration.
public function index()
{
    $columns = Model::getColumns();

    return view('models.index', compact('columns'));
}
  1. Create data action on controller.
public function data()
{
    return Model::getDatatable();
}
  1. Create route for data action.
Route::get('/models/data', 'ModelController@data')->name('models.data');
  1. 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>
  1. 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.

  1. Use ?only separated by commas to filter columns like 'select' in SQL
query: only=field1;field2;field3...
  1. Use ?search with key and value to apply where or whereHas
query: search=key:value

model: 
public static function searchable() {
    'key' => 'operator',
}

define searchable method on model is needed and relationships are identified by dot relation.column

  1. Use ?operators to change search operator dynamically
query: field1:operator1
  1. Use ?order to sort results
query: field:direction
  1. Use ?with to load relation data separated by commas
query: relation1;relation2;relation3...