phplaraveleloquentlaravel-backpack

Filtering with multiple views & Modified columns


I have an ApplicationCrudController that has a default view, and a custom view called "Simplified View" inside the list setup function. However, when I tried to apply my filters when I am in "Simplified View", I always get redirected back to the default, what should I do?

Additionally, in my simplified view, I grouped multiple rows into one, and then concatenated the "makeid" and "modelid" columns from the rows with same "partid" together. How should I change my make and model filters so that, when applied, the concatenated string gets regenerated with the filtered makes and models?

Thank you so much for your time, I appreciate any help!

//ApplicationCrudController.php
   public function setupSimplifiedView(){        

        CRUD::modifyColumn('regionid', [
            'name' => 'regionid',          // column name in the CRUD list view
            'label' => 'Region',    // label for the column
            'type' => 'select',
            'entity' => 'region',  // The relationship chain
            'attribute' => 'regionabbr',  // The column to display (region name)
            'model' => "App\Models\Region",  // The related model
            'relation_type' => 'BelongsTo',
        ]);

        CRUD::modifyColumn('positionid', [
            'name' => 'positionid',         
            'label' => 'Position',    
            'type' => 'select',
            'entity' => 'position',  
            'attribute' => 'Position',  
            'model' => "App\Models\Position",  
            'relation_type' => 'BelongsTo',
        ]);

       

        // CRUD::column('start')->type('text');
        // CRUD::column('end')->type('text');
        CRUD::column('updatetime')->type('datetime')->format('YYYY-MM-DD');
        CRUD::column('releasetime')->type('datetime')->format('YYYY-MM-DD');
        
        CRUD::addClause('where', 'del_flg', 0); // Hide soft deleted items
        CRUD::removeColumn('del_flg');
        CRUD::removeColumn('modelid');
        CRUD::removeColumn('makeid');
        CRUD::removeColumn('start');
        CRUD::removeColumn('end');
        CRUD::removeColumn('remark');
        CRUD::removeColumn('editor');

        CRUD::groupBy('partid');

        CRUD::addColumn([
            'name' => 'makes_and_models',
            'label' => 'Application',
            'type' => 'model_function', // Specify that this is a model function column
            'function_name' => 'getMakesAndModels', // The method defined in application model
            'limit' => 100,
        ])->afterColumn('positionid');

        $this->crud->denyAccess('delete');
        $this->crud->denyAccess('update');

    }


    /**
     * Define what happens when the List operation is loaded.
     * 
     * @see  https://backpackforlaravel.com/docs/crud-operation-list-entries
     * @return void
     */
    protected function setupListOperation()
    { 
        CRUD::setFromDb(); 

        CRUD::modifyColumn('modelid', [
            'name' => 'modelid',
            'label' => 'Model',
            'type' => 'select',
            'entity' => 'carmodel',  
            'attribute' => 'ModelName',   
            'model' => "App\Models\Carmodel", 
            'relation_type' => 'BelongsTo',
        ]);

        CRUD::modifyColumn('makeid', [
            'name' => 'makeid',
            'label' => 'Make',
            'type' => 'select',
            'entity' => 'make',  
            'attribute' => 'MakeName',   
            'model' => "App\Models\Make",  
            'relation_type' => 'BelongsTo',
        ]);



        CRUD::modifyColumn('regionid', [
            'name' => 'regionid',         
            'label' => 'Region',    
            'type' => 'select',
            'entity' => 'region',  
            'attribute' => 'regionabbr',  
            'model' => "App\Models\Region",
            'relation_type' => 'BelongsTo',
        ]);

        CRUD::modifyColumn('positionid', [
            'name' => 'positionid',         
            'label' => 'Position',    
            'type' => 'select',
            'entity' => 'position',  
            'attribute' => 'Position', 
            'model' => "App\Models\Position", 
            'relation_type' => 'BelongsTo',
        ]);

       

        CRUD::column('start')->type('text');
        CRUD::column('end')->type('text');
        CRUD::column('updatetime')->type('datetime')->format('YYYY-MM-DD');
        CRUD::column('releasetime')->type('datetime')->format('YYYY-MM-DD');
        
        CRUD::addClause('where', 'del_flg', 0); // Hide soft deleted items
        CRUD::removeColumn('del_flg');



        $this->runCustomViews([
            'setupSimplifiedView' => __('Simplified View'),
            //'setupDatabaseView' => __('Applications'),
            //..
        ]);
        

        // Filters

        CRUD::filter('region')
            ->type('select2')
            ->values(function() { return \App\Models\Region::all()->keyBy('regionid')->pluck('regionabbr', 'regionid')->toArray();}
            )
            ->whenActive(function ($value) {
                CRUD::addClause('where', 'regionid', $value);
            })->apply();

        CRUD::filter('make')
            ->type('select2')
            ->values(function() { 
                return \App\Models\Make::all()->keyBy('MakeID')->pluck('MakeName', 'MakeID')->toArray();}
            )
            ->whenActive(function ($value) {
                CRUD::addClause('where', 'MakeID', $value);
            })->apply();

        CRUD::filter('model')
            ->type('select2')
            // ->values(backpack_url('application/fetch/models'))
            // ->method('POST')
            ->values(function () {
                $makeId = request()->get('make');  
                if ($makeId) {
                    $modelIds = \App\Models\Basevehicle::where('MakeID', $makeId)
                                               ->pluck('ModelID')
                                               ->toArray();
                    return \App\Models\Carmodel::whereIn('ModelID', $modelIds)
                                               ->pluck('ModelName', 'ModelID')
                                               ->toArray();
                }
        
                return [];
            })
            ->whenActive(function ($value) {
                CRUD::addClause('where', 'ModelID', $value);
            })->apply();
        
//Application.php
// ...
public function getMakesAndModels() {
        $partId = $this->partid;
        $applications = Application::where('partid', $partId)->where('del_flg', 0)->get(); // Not deleted & partid is equal

            $first = true; //first occurence of the application
            $applicationStr = '';
            
            $count = 0;
            $prevModel = '';
            $prevMake = '';
            $altStr = '';

            foreach ($applications as $application) {
                if ($count >= 5) {
                    break; //limit the application models to less than five
                }
                $make = \App\Models\Make::where('MakeID', $application->makeid)->value('MakeName');
                $model = \App\Models\Carmodel::where('ModelID', $application->modelid)->value('ModelName');
                
                
                if ($application->start == $application->end) {
                    $yearStr = substr($application->start, 2);
                } else {
                    $yearStr = substr($application->start, 2) . '-' . substr($application->end, 2);
                }

                $carStr = '';

                if (strcmp($make, $prevMake) != 0) { //not equal to previous make
                    $carStr .= ' '.$make;
                    $count++;
                }

                if (strcmp($model, $prevModel) != 0) { //not equal to previous model
                    $carStr .= ' '.$model;
                }

                // Append each row of info to the application string
                // First occrerence without comma
                if ($first) {
                    $applicationStr .= $yearStr. $carStr;
                    $first = false;
                } else {
                    $applicationStr .= ', ' . $yearStr. $carStr;
                }

                $prevModel = $model;
                $prevMake = $make;
            }

            return $applicationStr; 
    }

I tried to add the filters inside the simplifiedView, but it stayed the same.


Solution

  • Custom Views (for ListOperation) do not support filters in the current version v6, maybe in the future, like in v7.