我與laravel 5.3試圖創建一個URL喜歡上了正確的日期,但是,不告訴我正確的數據Laravel不顯示我的頁面
本地主機/遊戲/ ID-NameGame-控制檯
我曾嘗試:
public function getGame($slug) {
$game = \DB::table('games')->first();
$info_game = \DB::table('info_games')->where('id_game', '=', $game->id_game)->first();
$console = \DB::table('console')->where('id', '=', $info_game->id_console)->first();
$slug = $game->id.'-'.$game->slug.'-'.$console->abb_cat;
return view('front.pages.game', compact('game','info_game','console'));
}
數據庫表由在:
game
*----------------*
id | slug
1 | the-order
5 | uncharted
*----------------*
info_game
*-------------------------*
id | console_id | id_games
1 | 2 | 5
*-------------------------*
這樣一來,URL成爲:
本地主機/遊戲/ 5-未知-PS4
因爲console_id 2 = PS4
他給我的頁面,但是,我展示的遊戲數據ID 1即秩序,而不是祕境
更新
通過下面的評論中給出的建議,我決定走這條路線:
web.php
Route::get('game/{id}-{slug}-{cons}', '[email protected]');
FrontController.php
公共職能getGame($ ID,$蛞蝓,$缺點){
$game = \DB::table('games')->where('slug', '=', $slug)->first();
$info_game = \DB::table('info_games')->where('id_game', '=', $game->id)->first();
$console = \DB::table('console')->where('id', '=', $info_game->id_console)->first();
$id = $game->id;
$cons = $console->abb_cat;
但我不明白爲什麼當我嘗試訪問該頁面時,它會自動混合我的數據:
在HandleExceptions->的HandleError(8, '試圖讓 非對象的屬性', '\應用\ HTTP \控制器\ FrontController.php', 40,陣列( 'ID'=> '1','蛞蝓'=> '的','缺點'=> '順序-1886-PS4', '遊戲'=>在FrontController.php線爲null))40
但它必須是: id:1 slug:the-order-1886 cons:ps4
是被傳入嵌塞是'5 -uncharted-ps4'。你永遠不會在你的函數的任何地方使用你的slu to來獲得遊戲,你只是第一排。 – aynber