背包预选 选择带有 $_GET 参数的过滤器



我有一个文档和帖子(用于该文档(背包 CRUD 资源。我想在帖子数据表中创建一个按钮,以便当我单击该按钮时,它将链接到该帖子的文档。

这是我的模特帖子

class Post extends Model{
    ...
    function allDocuments(){
        return '<a href="/admin/document/?post_id='.$this->id.'" class="btn btn-xs btn-default"><i class="fa fa-list"></i> Documents</a>';
    }
}

这是我的PostCrudController

class PostCrudController extends CrudController{
    ...
    $this->crud->addButtonFromModelFunction('line', 'all_documents', 'allDocuments', 'beginning');
}

这就是我的DocumentCrudController

class DocumentCrudController extends CrudController{
    ...
            $this->crud->addFilter([ 
              'name' => 'post_id',
              'type' => 'select2',
              'label'=> 'Post To Filter',
              'value' => $_GET['post_id'] // <------- Hoped this works, but doesn't
            ], function() {
                $condos = [];
                foreach(Post::get() as $c){
                    $condos[$c->id] = $c->title." >> ".$c->category->name." >> ".$c->category->condomimium->name;
                }
                return $condos;
            }, function($value) { // if the filter is active
                $this->crud->addClause('where', 'post_id', $value);
            });
}

我已经看到背包数据表使用带有参数的 POST 请求(在我的例子中是post_id(来过滤数据表,但我需要调用 GET 请求来预先选择带有数据表结果的过滤器。

提前致谢

为什么不在 laravel 中使用请求? 像这样的东西$request->input(post_id);并在这样的方法中实例化请求

use IlluminateHttpRequest;
public function store(Request $request) { // logic here }

实际上即使没有'value' => $_GET['post_id']也可以工作。背包识别自动过滤。筛选器的名称必须与GET参数的名称匹配

设法获得类似工作,任何改进都非常感谢。

我需要制作一个链接到子模型的列,并且只显示表中的链接项目,因此我把它写在一个特征中

受保护的函数 redirect_filter_column($_name, $_attribute, $_query({ $this->crud->addColumn([ 'name' => $name, 'label' => ucwords(Str::replace('', ' ', $_name((, 'type' => 'select', 'attribute' => $_attribute, 'entity' => $_name, 'wrapper' => [ 'element' => 'a', '

href' => 函数 ($crud, $column, $entry, $related_key( 使用 ($_query({ return backpack_url( $column['entity'] .'?' .$_query .'=' .$related_键 ."&id" .'=' .$related键(;}, ] ]);

在相关(属于(模型的 crud 控制器中,我使用它来过滤`$query = $this->crud->query;

if (request()->get('parent_id')) {
    $query->orWhere('parent_id', request()->get('parent_id'));
}
if (request()->get('id')){
    $query->orWhere('id', request()->get('id'));
}
$this->crud->query = $query;

'

它对我有用

最新更新