简体   繁体   English

将参数传递给 crud controller 背包 laravel

[英]Pass parameters to crud controller Backpack laravel

i create button in the line of the table "league" and when press on it most moved to the table "game" and pass parameter "league_id" to GameCrudController... then when i get the "league_id" in setup function on GameCrudController i have problem, to find the problem i make error_log on setup function to know what happen, i well set the code and some image to show the problem:我在表格“联盟”的行中创建按钮,当按下它时,它最移动到表格“游戏”并将参数“league_id”传递给 GameCrudController ...然后当我在 GameCrudController 上的设置 function 中获得“league_id”时有问题,要找到问题,我在设置 function 上创建 error_log 以了解发生了什么,我很好地设置了代码和一些图像来显示问题:

added button in the table "league":在“联赛”表中添加按钮:

$this->crud->addButtonFromView("line", "Games", "games" ,'end');

show button in view league table在视图排名表中显示按钮

create games.blade.php view in vendor\backpack\crud\src\resources\views\crud\buttons\games.blade.php, and the content is:在 vendor\backpack\crud\src\resources\views\crud\buttons\games.blade.php 中创建 games.blade.php 视图,内容为:

@if ($crud->hasAccess('games'))
        <a href='{{ backpack_url('game/' . $entry->getKey()) }}' class="btn btn-sm btn-link"><i class="fa fa-edit"></i>Games</a>
@endif

added in the route:在路线中添加:

Route::crud('game/{league_id?}', 'GameCrudController');

and in the setup function on GameCrudController:并在 GameCrudController 上的设置 function 中:

public function setup()
{
    $this->crud->setModel('App\Models\Game');
    $this->crud->setRoute(config('backpack.base.route_prefix') . '/game');
    $this->crud->setEntityNameStrings('game', 'games');


    $passed_league_id = \Route::current()->parameter('league_id');
    error_log("test");
    error_log($passed_league_id);
}

and in console its access one time to "league_id" and then show empty line after "test" like this:并在控制台中访问一次“league_id”,然后在“test”之后显示空行,如下所示:

test
3
.
.
.
test
"empty line"
.
.
.
test
"empty line"
.
.
.

i don't andurstand why print "test" three times and i can't access the passed_id?!我不明白为什么要打印三遍“test”而我无法访问passed_id?!

Please help.请帮忙。 I want to reach for "league_id" to make filter at GameCrudController.我想在 GameCrudController 上找到“league_id”来制作过滤器。 What is the problem in the previous steps and thanks for the help.前面步骤中的问题是什么,感谢您的帮助。

What you're seeing is normal.你看到的很正常。 backpack for Laravel uses ajax to get the data later on. Laravel 的背包使用 ajax 稍后获取数据。

at the first request: your URL looks like this:在第一个请求:您的 URL 看起来像这样:

/admin/game/1

where 1 is league_id .其中1league_id

after the first request backpack calls ajax requests to show the data.在第一次请求背包调用 ajax 请求显示数据后。 and looks like it's calling看起来它在打电话

/admin/game/search

instead of代替

/admin/game/1/search

to repair that you should use setRoute correctly:修复你应该正确使用setRoute

$passed_league_id = \Route::current()->parameter('league_id');
if($passed_league_id != null )
{
    $this->crud->setRoute(config('backpack.base.route_prefix') . '/game/'.$passed_league_id);
}
else 
{
    $this->crud->setRoute(config('backpack.base.route_prefix') . '/game');
}

this way it'll generate the routes correctly and all of you're following ajax requests will be made on the base url这样,它将正确生成路线,并且你们所有人都在关注 ajax 请求将在基础 url 上进行

/admin/game/1

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

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