【发布时间】:2017-04-04 12:24:36
【问题描述】:
我正在尝试使用 laravel 5.3 创建一个类似的 url,但是没有显示正确的数据
localhost/game/ID-NameGame-Console
我试过了:
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 变成了:
localhost/game/5-uncharted-ps4
因为 console_id 2 = ps4
他向我展示了该页面,但向我展示了 ID 为 1 的游戏数据,即 The Order 而不是 Uncharted
更新
按照 cmets 给出的建议,我决定走这条路:
web.php
Route::get('game/{id}-{slug}-{cons}', 'FrontController@getGame');
FrontController.php
公共函数 getGame($id,$slug,$cons) {
$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;
但我不明白为什么当我尝试访问该页面时,它会自动混合我的数据:
at HandleExceptions->handleError(8, '试图获取的属性 非对象', '\app\Http\Controllers\FrontController.php', 40、array('id' => '1', 'slug' => 'the', 'cons' => 'order-1886-ps4', 'game' => null)) 在 FrontController.php 第 40 行
但它必须是: 编号:1 蛞蝓:the-order-1886 缺点:ps4
【问题讨论】:
-
传入的 slug 是
5-uncharted-ps4。你永远不会在函数中的任何地方使用你的 slug 来获取游戏,你只是得到了第一行。