Home > Back-end >  Laravel get one record from the database
Laravel get one record from the database

Time:05-25

Im trying to make a list with game developers, and when you click on one of them you can see the games they have made. I only dont know how to get further....

This is what i have now:

Index.blade.php:

<table >
<thead >
<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>

I already tried some methods like these:

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

and

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);
}

Never did this before.. so hope im on the right way :)

CodePudding user response:

Laravel controller methods like show will use 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.

So you wont have to write another query to show your Gamedev:

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