我有一个 ApplicationCrudController,它有一个默认视图,以及一个在列表设置函数内称为“简化视图”的自定义视图。但是,当我在“简化视图”中尝试应用过滤器时,我总是被重定向回默认设置,我该怎么办?
此外,在我的简化视图中,我将多行分组为一行,然后将具有相同“partid”的行中的“makeid”和“modelid”列连接在一起。我应该如何更改我的品牌和型号过滤器,以便在应用时,使用过滤后的品牌和型号重新生成连接的字符串?
非常感谢您的宝贵时间,非常感谢您的帮助!
//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;
}
我尝试在简化视图中添加过滤器,但它保持不变。
自定义视图(用于ListOperation)当前版本v6不支持过滤器,也许将来会支持,就像v7一样。