Laravel-未定义的属性:Illuminate \ Database \ MySqlConnection :: $ username

时间:2019-06-02 23:25:53

标签: laravel

我编写了一个查询,以将参数从视图传递到控制器,但出现此错误:

  

未定义的属性:Illuminate \ Database \ MySqlConnection :: $ username

查看传递参数

<td width="25%"><a class="btn btn-info" href="{{ route('gamesListDetail',$game->id) }}">{{ $game->name }}</a></td>

接收控制器

    public function gamesListDetail($id = null)
    {
    $gamelists = DB::table("platform_games")
        ->select("platform_games.id", "platform_games.username","game_player.game_id")
        ->join("game_player","game_player.game_id","=","platform_games.id")
            ->where('platform_games.id',$id)
            ->take(5);
        return view('soccerrave.games.gamesListDetail', compact('gamelists'));
    }    

接收视图

    <tbody>
        @foreach($gamelists as $key => $gamelist)
            <tr>
                <td>{{ ++$key }}</td>

                <td>{{ $gamelist->username }}</td>


            </tr>
        @endforeach
            <tr>
            <td colspan="8">
                {{ $gamelists->links() }}
            </td>
            </tr>         
    </tbody>

我希望该视图根据该参数显示前5个数据。但是我遇到了这个错误:

  

未定义的属性:Illuminate \ Database \ MySqlConnection :: $ username

1 个答案:

答案 0 :(得分:1)

Documentation说:

  

跳过/获取

     

要限制查询返回的结果数,或跳过   根据查询中给定的结果数,您可以使用skiptake   方法:

     

$users = DB::table('users')->skip(10)->take(5)->get();

因此,通过示例,我们看到在take方法之后必须进行get()调用。

因此,请修改您的代码:

public function gamesListDetail($id = null)
{
    $gamelists = DB::table("platform_games")
                     ->select(
                       "platform_games.id", 
                       "platform_games.username",
                       "game_player.game_id"
                     )
                     ->join(
                       "game_player",
                       "game_player.game_id", "=", "platform_games.id"
                     )
                     ->where('platform_games.id', $id)
                     ->take(5)
                     ->get(); // this one is required

    return view('soccerrave.games.gamesListDetail', compact('gamelists'));
}