简体   繁体   English

背包laravel-使用$ _GET参数预选选择过滤器

[英]backpack laravel - preselect select filter with $_GET param

I have a Document and Post (for that document) Backpack CRUD resources. 我有一个Document and Post(针对该文档)背包CRUD资源。 I want to create a button inside the post datatable so that when I click that button, it will link to the documents of that post. 我想在发布数据表中创建一个按钮,以便当我单击该按钮时,它将链接到该发布的文档。

this is my model post 这是我的模特帖子

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>';
    }
}

this is my PostCrudController 这是我的PostCrudController

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

}

and that's my DocumentCrudController 那就是我的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);
            });
}

I've seen that backpack datatables uses a POST request with the parameters (in my case post_id ) to filter the datatable, but I need to call a GET request to preselect my filter with the datatable result accordingly. 我已经看到背包数据表使用带有参数(在我的情况下为post_id )的POST请求来过滤数据表,但是我需要调用GET请求以预先选择具有数据表结果的过滤器。

thanks in advance 提前致谢

Why not using Request in laravel ? 为什么不在laravel中使用Request? something like this $request->input(post_id); 像这样的$request->input(post_id); and instantiate the request in the method like this 并以这种方法实例化请求

use Illuminate\Http\Request;

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

Actually it works even without 'value' => $_GET['post_id'] . 实际上,即使没有'value' => $_GET['post_id'] Backpack recognizes automatically filter. 背包会自动识别过滤器。 The name of the filter must match to the name of the GET param 过滤器的名称必须与GET参数的名称匹配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM