skip to Main Content

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' => "AppModelsRegion",  // The related model
            'relation_type' => 'BelongsTo',
        ]);

        CRUD::modifyColumn('positionid', [
            'name' => 'positionid',         
            'label' => 'Position',    
            'type' => 'select',
            'entity' => 'position',  
            'attribute' => 'Position',  
            'model' => "AppModelsPosition",  
            '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' => "AppModelsCarmodel", 
            'relation_type' => 'BelongsTo',
        ]);

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



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

        CRUD::modifyColumn('positionid', [
            'name' => 'positionid',         
            'label' => 'Position',    
            'type' => 'select',
            'entity' => 'position',  
            'attribute' => 'Position', 
            'model' => "AppModelsPosition", 
            '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 AppModelsRegion::all()->keyBy('regionid')->pluck('regionabbr', 'regionid')->toArray();}
            )
            ->whenActive(function ($value) {
                CRUD::addClause('where', 'regionid', $value);
            })->apply();

        CRUD::filter('make')
            ->type('select2')
            ->values(function() { 
                return AppModelsMake::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 = AppModelsBasevehicle::where('MakeID', $makeId)
                                               ->pluck('ModelID')
                                               ->toArray();
                    return AppModelsCarmodel::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 = AppModelsMake::where('MakeID', $application->makeid)->value('MakeName');
                $model = AppModelsCarmodel::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.

2

Answers


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

    Login or Signup to reply.
  2.     // Load simplified view and add a custom parameter
    return redirect()->to('application?view=simplified');
    
    //Modify your setupListOperation() to check for the view=simplified parameter:
    
        protected function setupListOperation()
    {
        $view = request()->get('view');
        if ($view == 'simplified') {
            $this->setupSimplifiedView();
        } else {
            CRUD::setFromDb();  // Default view setup
        }
    }
    

    // Step 1: Modify Filters to Work with Grouped Data
    Ensure your filters apply correctly for the makeid and modelid and that they are applied to the grouped data. Here’s an example:

        CRUD::filter('make')
        ->type('select2')
        ->values(function() { 
            return AppModelsMake::all()->keyBy('MakeID')->pluck('MakeName', 'MakeID')->toArray(); 
        })
        ->whenActive(function ($value) {
            // Apply filter only for the rows that match the make
            CRUD::addClause('where', 'makeid', $value);
        })->apply();
    
    CRUD::filter('model')
        ->type('select2')
        ->values(function () {
            $makeId = request()->get('make');  
            if ($makeId) {
                $modelIds = AppModelsBasevehicle::where('MakeID', $makeId)->pluck('ModelID')->toArray();
                return AppModelsCarmodel::whereIn('ModelID', $modelIds)->pluck('ModelName', 'ModelID')->toArray();
            }
            return [];
        })
        ->whenActive(function ($value) {
            CRUD::addClause('where', 'modelid', $value);
        })->apply();
    
    //Step 2: Regenerate the Concatenated String in getMakesAndModels()
    Now, modify the getMakesAndModels() method in the Application model so that it regenerates the concatenated makeid and modelid values after the filters have been applied.
    
        public function getMakesAndModels() {
        $partId = $this->partid;
    
        // Apply filters if present
        $applicationsQuery = Application::where('partid', $partId)->where('del_flg', 0);
        
        if (request()->has('make')) {
            $applicationsQuery->where('makeid', request()->get('make'));
        }
        
        if (request()->has('model')) {
            $applicationsQuery->where('modelid', request()->get('model'));
        }
        
        $applications = $applicationsQuery->get();  // Get filtered applications
        
        $applicationStr = '';
        $prevMake = '';
        $prevModel = '';
        $count = 0;
        $first = true;
    
        foreach ($applications as $application) {
            if ($count >= 5) {
                break;
            }
    
            $make = AppModelsMake::where('MakeID', $application->makeid)->value('MakeName');
            $model = AppModelsCarmodel::where('ModelID', $application->modelid)->value('ModelName');
    
            $yearStr = $application->start == $application->end ? substr($application->start, 2) : substr($application->start, 2) . '-' . substr($application->end, 2);
            $carStr = '';
    
            if ($make != $prevMake) {
                $carStr .= ' ' . $make;
                $count++;
            }
    
            if ($model != $prevModel) {
                $carStr .= ' ' . $model;
            }
    
            if ($first) {
                $applicationStr .= $yearStr . $carStr;
                $first = false;
            } else {
                $applicationStr .= ', ' . $yearStr . $carStr;
            }
    
            $prevMake = $make;
            $prevModel = $model;
        }
    
        return $applicationStr;
    }
    

    // Session State (Alternative Approach): If you don’t want to pass the view state in the URL, you could store the current view in the session and check for it in your controller methods.

        if (session()->has('view')) {
        $view = session()->get('view');
        if ($view == 'simplified') {
            $this->setupSimplifiedView();
        }
    } else {
        CRUD::setFromDb();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search