2017-04-04 47 views
0

我與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

+0

是被傳入嵌塞是'5 -uncharted-ps4'。你永遠不會在你的函數的任何地方使用你的slu to來獲得遊戲,你只是第一排。 – aynber

回答

0

我下決心:

preg_match_all("/([^\-]+)\-(.+)\-(.+)/",$fatSlug,$parts); 

$id = $parts[1]; 
$slug = $parts[2]; 
$cons = $parts[3]; 

由於Snapey laracasts的

0

你的表game沒有id_game專欄也idconsole沒有id_console它是console_id讓你查詢

$info_game = \DB::table('info_games')->where('id_game', '=', $game->id_game)->first(); 
$console = \DB::table('console')->where('id', '=', $info_game->id_console)->first(); 

應該

$info_game = \DB::table('info_games')->where('id_game', '=', $game->id)->first(); 
$console = \DB::table('console')->where('id', '=', $info_game->console_id)->first(); 

,如果你想生成一些遊戲的slu g。你必須正確選擇它。你總是$game = \DB::table('games')->first();選擇的第一場比賽,你已經使用類似$game = \DB::table('games')->where('slug' ,'=', $slug)->first();

+0

它的工作原理,但不幸的是繼續採用ID爲1的遊戲數據,當我在blade.php中檢索'{{$ game-> title}}'時,他釋放了「The Order」而不是「Uncharted」。 。 – Xanger

+0

你的子彈形式如何是'ID-NameGame-Console',而實際數據是'order' –

+0

所以沒有辦法像''order-order''那樣創建一個從slug開始的URL?我試過這樣的東西,雖然很醜(用作測試...)'$ link = \t $ gioco-> id .'-'。$ gioco-> slug .'-'。$ console-> abb_cat; ' 只有那麼,我在桌面遊戲的「哪裏」放了什麼? – Xanger