簡體   English   中英

將參數傳遞給 crud controller 背包 laravel

[英]Pass parameters to crud controller Backpack laravel

我在表格“聯盟”的行中創建按鈕,當按下它時,它最移動到表格“游戲”並將參數“league_id”傳遞給 GameCrudController ...然后當我在 GameCrudController 上的設置 function 中獲得“league_id”時有問題,要找到問題,我在設置 function 上創建 error_log 以了解發生了什么,我很好地設置了代碼和一些圖像來顯示問題:

在“聯賽”表中添加按鈕:

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

在視圖排名表中顯示按鈕

在 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

在路線中添加:

Route::crud('game/{league_id?}', '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);
}

並在控制台中訪問一次“league_id”,然后在“test”之后顯示空行,如下所示:

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

我不明白為什么要打印三遍“test”而我無法訪問passed_id?!

請幫忙。 我想在 GameCrudController 上找到“league_id”來制作過濾器。 前面步驟中的問題是什么,感謝您的幫助。

你看到的很正常。 Laravel 的背包使用 ajax 稍后獲取數據。

在第一個請求:您的 URL 看起來像這樣:

/admin/game/1

其中1league_id

在第一次請求背包調用 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM