PHP code example of winex01 / backpack-filter

1. Go to this page and download the library: Download winex01/backpack-filter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

winex01 / backpack-filter example snippets



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

    public function setup()
    {
        CRUD::setModel(\App\Models\SampleModel::class);
        CRUD::setRoute(config('backpack.base.route_prefix') . '/sample');
        CRUD::setEntityNameStrings('sample', 'samples');
    
        $this->crud->allowAccess('filters'); // Allow access
    }

    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
        ]);
    }

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.
        }
    });

    //CRUD::setFromDb(); 
    CRUD::setFromDb(false, true); //by doing this, it will remove all those fields that was automatically added by backpack

    // or dont use CRUD::setFromDb(false, true) and just manually add columns each
    $this->crud->columns('testColumn');
    // more columns etc...
}

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());
}

// 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...
}


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