【问题标题】:Laravel do not show me the correct date on the pageLaravel 没有在页面上显示正确的日期
【发布时间】: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 来获取游戏,你只是得到了第一行。

标签: php laravel-5


【解决方案1】:

您的表 game 没有 id_game 列它有 id 并且在 console 没有 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();

并且,如果您尝试为某些游戏生成 slug。你必须正确选择它。你总是选择$game = \DB::table('games')->first();的第一个游戏你必须使用$game = \DB::table('games')->where('slug' ,'=', $slug)->first();之类的东西

【讨论】:

  • 它有效,但不幸的是继续获取​​ id 为 1 的游戏数据,当我在 Blade.php 中检索 {{ $game->title }} 时,他发布了“The Order”而不是“Uncharted”...
  • 你的 slug 表单是ID-NameGame-Console,真实数据是the-order
  • 所以没有办法像从"the-order"这样的slug开始创建一个URL?我尝试了这样的东西,虽然丑陋(用作测试......)$link = $gioco->id.'-'.$gioco->slug.'-'.$console->abb_cat;只有这样,我在桌面游戏的“哪里”放了什么?
  • 你在这里有很多方法:1. 是使用一个独特的 slug 像the-order 没有这些 id,Console 然后你将使用where() 中提到的答案。 2.使用像/{id}/{slug}/{slug}这样的url参数,然后在控制器中你必须使用这些参数作为public function getGame($id, $slug, $console),这样你就可以使用三个变量了。 3.解析我不推荐的slug字符串
  • 如果你试图解析像 ID-NameGame-Console 这样的 slug 像 the-order 这样处理 NameGame 因为它会给你一个像 1-the-order-ps4 这样的 slug 所以你不能爆炸字符串与-。相反,您必须确保 NameGame 永远不会有 -s
【解决方案2】:

我决定:

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

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

感谢 laracasts 的 Snapey

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 2018-03-14
    • 1970-01-01
    相关资源
    最近更新 更多