我在 "联赛 "表的行中创建了一个按钮,当按下它时,它被移动到 "游戏 "表,并将参数 "league_id "传递给GameCrudController......然后当我在GameCrudController的设置函数中得到 "league_id "时,我遇到了问题,为了找到问题,我在设置函数中制作了错误日志,以了解发生了什么,我设置了代码和一些图片来显示问题。
在 "联赛 "表中添加按钮。
$this->crud->addButtonFromView("line", "Games", "games" ,'end');
在供应商处创建games.blade.php视图/backpack/crud/src/resources/views/crud/buttons/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
在路由中添加。
Route::crud('game/{league_id?}', 'GameCrudController');
并在GameCrudController上的设置函数中添加。
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);
}
在控制台中,它访问了一次 "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 ?
请帮助我。I want to reach for "league_id" to make filter at GameCrudController.What is the problem in the previous steps and thanks for the help.
你所看到的是正常的. Backpack for Laravel使用ajax来获取数据.
在第一次请求时: 你的URL看起来像这样:
/admin/game/1
你的URL看起来是这样的: where 1
是 league_id
.
在第一个请求之后,backpack调用ajax请求来显示数据,看起来像是在调用
/admin/game/search
而不是
/admin/game/1/search
修,你应该使用 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');
}
这样它就会正确地生成路由,而你所有的ajax请求都会在基本的url上进行。
/admin/game/1