Laravel 从数据库中获取一条记录

Laravel get one record from the database

我正在尝试制作一个游戏开发者列表,当您单击其中一个时,您可以看到他们制作的游戏。我只是不知道如何更进一步....

这是我现在拥有的:

Index.blade.php:

<table class="table">
<thead class="thead-dark">
<tr>
    <th scope="col">#</th>
    <th scope="col">Naam</th>
    <th scope="col">Opgericht in:</th>
</tr>
</thead>
<tbody>
@foreach($gamedevs as $gamedev)
<tr>
    <td>{{ $gamedev['id'] }} </td>
    <td><a href="">{{ $gamedev['naam'] }}</a></td>
    <td>{{ $gamedev['opgericht'] }}</td>
</tr>
@endforeach
</tbody>

我已经尝试过一些类似的方法:

href="{{route('games.show', ['id'=>$gamedev->id])}}

href="/games/{{$gamedev['id']}}

listcontroller.php

public function index()
    {
        $gamedevs = Gamedevs::all();

        return view("index", [ 'gamedevs' => $gamedevs]);

    }


public function show(Gamedevs $gamedevs)
{
    $gamedevs = Gamedevs::where($gamedevs)->first();

    return view('pages.games.show')->with('Gamedevs',$gamedevs);
}

以前从未这样做过..所以希望我走对了路:)

Laravel 控制器方法如 show 将使用 service container to resolve all the parameters you define there. Since you define a model, laravel will automatically find the one corresponding with explicit model binding.

因此您不必编写另一个查询来显示您的 Gamedev:

public function show(Gamedevs $gamedevs)
{
    return view('pages.games.show')->with('Gamedevs',$gamedevs);
}

web.php

中的示例路线

路线::get('/games/{dev_id}','listController@show');

您的 listController.php

中的示例方法

public 函数显示($gamedevs)

{

#假设您的 $gamedevs 是您的开发者 ID,那么请以他们的 ID 为目标

$devs = Gamedevs::where('id',$gamedevs)->first();

return view('pages.games.show')->with([
  'Gamedevs'=>$devs
]);

}

在您看来

href="/games/{{$Gamedevs->id}}