winex01/backpack-filter

Laravel backpack free filter without buying pro version.

2.0.2 2024-10-05 06:50 UTC

This package is auto-updated.

Last update: 2024-10-07 11:35:00 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();
//or 
CRUD::setFromDb(false, true); //by doing this, it will remove all those fields that was automatically add by backpack

Allow access:

$this->crud->allowAccess('filters');

You have 2 options how to add the button.

Option 1: If you dont want to modify list.blade.php bec. you want to get the latest update from backpack future version you can add it like this, as button. The filter will be located at the col-md-9 before the search input box in the upper right.

// setupListOperation method
CRUD::button('filters')->view('winex01.backpack-filter::buttons.list_top_collapse');

Option 2: Create a file resources/vendor/backpack/crud/list.blade.php and paste the original backpack file contents. You can download the file here: list.blade.php Inside, add this line:

// publish the config file and set: winex01/backpack-filter.php
'auto_add_button' => false, // set to false so we can add it manually

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

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

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_from_array',
            '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...
}

Publish config

php artisan vendor:publish --provider="Winex01\BackpackFilter\BackpackFilterServiceProvider" --tag="config"

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.