winex01/backpack-filter

Laravel backpack free filter without buying pro version.

1.1.8 2024-08-26 13:32 UTC

This package is auto-updated.

Last update: 2024-08-26 13:33:36 UTC


README

Latest Version on Packagist Total Downloads The Whole Fruit Manifesto

This package provides a filter functionality for Backpack for Laravel administration panel. If you don't have the budget or haven't purchased the pro version, this is a great alternative for implementing filters.

Screenshots

bootstrap5 bootstrap4

Theme Supported

  • theme-coreuiv2 - YES
  • theme-coreuiv4 - YES
  • theme-tabler - YES

Supported Fields

  • Free Backpack Fields(except relationship field)
  • date_range (this is custom so it has limited customization, can change wrapper and attributes)

Installation

Via Composer

composer require winex01/backpack-filter

Usage

Make sure to remove this, or manually enter the columns, or unset list.field in list operation:

//CRUD::setFromDb();

Create a file resources/vendor/backpack/crud/list.blade.php and paste the original backpack file contents. Inside, add this line:

//resources/vendor/backpack/crud/list.blade.php
@include('winex01.backpack-filter::buttons.list_top_collapse')

{{-- Backpack List Filters --}}
// some code here...

OR you can download the file here: list.blade.php

//line 51
@include('winex01.backpack-filter::buttons.list_top_collapse')

To use the filter this package provides, inside your EntityCrudController do:

class EntityCrudController extends CrudController
{
    use \Winex01\BackpackFilter\Http\Controllers\Operations\FilterOperation;

    // method setup....

    public function setupFilterOperation()
    {
        $this->crud->field([
            'name' => 'status',
            'label' => __('Status'),
            'type' => 'select',
            'options' => [
                1 => 'Connected',
                2 => 'Disconnected'
            ],
            'wrapper' => [
                'class' => 'form-group col-md-6'
            ]
        ]);

        // 
        $this->crud->field([
            'name' => 'date_range',
            'label' => __('Date Range'),
            'type' => 'date_range',
            // although this is a custom field, you can still use the wrapper and attribute here
        ]);
    }

To apply the filter field into queries, inside your setupListOperation:

public function setupListOperation()
{
    // if you use this method closure, validation is automatically applied.
    $this->filterQueries(function ($query) {
        $status = request()->input('status');
        $dates = request()->input('date_range');

        if ($status) {
            $query->where('status_id', $status);
        }

        if ($dates) {
            $dates = explode('-', $dates);
            //$query->where... your clause here or scope.
        }
    });

    // some code here... add column etc...
}

If you want to make your own validation:

public function filterValidations()
{   
    // If no access to filters, then don't proceed but don't show an error.
    if (!$this->crud->hasAccess('filters')) {
        return false;
    }

    // if you dont want to use validator and want to use request file, modify below, up to you.

    $validationErrors = [];

    // validator here.

    if (!empty($validationErrors)) {
        \Alert::error($validationErrors)->flash();
        return redirect()->back();
    }

    return redirect()->back()->withInput(request()->input());
}

This package also provides with export using https://laravel-excel.com/, this operation automatically add entity/export route, be sure you have EntityExport.php file in your export directory. example if you have UserCrudController, you must have app/Exports/UserExport.php file. Also if you have an active filters it will also apply into the export.

// crud controller
class UserCrudController extends CrudController
{
    use \Winex01\BackpackFilter\Http\Controllers\Operations\ExportOperation;

    // Optional: if you dont want to use the entity/export or user/export convention you can override the export route:
    public function exportRoute()
    {
        return route('test.export');; // if you define a route here then it will use instead of the auto
    }    

    // setup method...
}

Change log

Changes are documented here on Github. Please see the Releases tab.

Testing

composer test

Contributing

Please see contributing.md for a todolist and howtos.

Security

If you discover any security related issues, please email winnie131212592@gmail.com instead of using the issue tracker.

Credits

License

This project was released under MIT, so you can install it on top of any Backpack & Laravel project. Please see the license file for more information.

However, please note that you do need Backpack installed, so you need to also abide by its YUMMY License. That means in production you'll need a Backpack license code. You can get a free one for non-commercial use (or a paid one for commercial use) on backpackforlaravel.com.