8000 fix 支持筛选条件默认选中时获取关联数据项 · laraveladmin-cn/laraveladmin@f83c4bc · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f83c4bc

Browse files
committed
fix 支持筛选条件默认选中时获取关联数据项
1 parent 8e1fda4 commit f83c4bc

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

app/Http/Controllers/Traits/ResourceController.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,44 @@ public function index()
7474
collect($this->sizer)->keys()->filter(function ($value) {
7575
return Str::endsWith($value, '_id');
7676
})->map(function ($value) use (&$data) {
77-
$val = str_replace('_id', '', $value);
77+
$val = collect(explode('.',$value))->last();
78+
$val = Str::replaceLast('_id', '', $val);
7879
$model = '\App\Models\\' . Str::studly($val);
7980
$item = [];
80-
$relation_id = Request::input('where.' . $value, 0);
81+
$where = Request::input('where');
82+
$relation_id = isset($where[$value])?$where[$value]:0;
8183
if ($relation_id && class_exists($model)) {
82-
$fields = collect(isset($this->mapsWhereFields[$value]) ? $this->mapsWhereFields[$value] : ['id', 'name'])
84+
$fields_and_relation = collect(isset($this->mapsWhereFields[$value]) ? $this->mapsWhereFields[$value] : ['id', 'name'])
8385
->map(function ($field){
8486
if(is_string($field) && Str::contains($field,'`')){
8587
return DB::raw($field);
8688
}
8789
return $field;
88-
})->toArray();
89-
$item = collect($model::select($fields)
90-
->find($relation_id))->toArray();
90+
});
91+
$fields = $fields_and_relation->filter(function ($item){
92+
return !is_array($item);
93+
})->toArray();
94+
$relation = $fields_and_relation->filter(function ($item){
95+
return is_array($item);
96+
})->map(function ($item){
97+
return function ($q)use($item){
98+
if($item){
99+
$q->select($item);
100+
}
101+
return $q;
102+
};
103+
})->toArray();
104+
$modelObj = $model::query();
105+
if($fields){
106+
$modelObj = $modelObj->select($fields);
107+
}
108+
if($relation){
109+
$modelObj = $modelObj->with($relation);
110+
}
111+
$item = collect($modelObj->find($relation_id))->toArray();
91112
}
92-
$data['maps'][$value] = $item ? [$item] : [];
113+
$map = $item ? [$item] : [];
114+
$data['maps'][$value] = $map;
93115
});
94116
//条件筛选及排序返回
95117
$this->addOptions();

0 commit comments

Comments
 (0)
0