0

私はLaravel 8で作業しており、Bladeで次のようなテーブルを作成しました:

<div class="card-body table-responsive p-0">
    <table class="table table-hover">
        <tr>
            <th>Username</th>
            <th>Email</th>
            <th>Role</th>
            <th>Actions</th>
        </tr>
        @foreach($roles as $role)
            @if(count($role->users))
                @foreach($role->users as $user)
                    <tr>
                        <td>{{ $user->name }}</td>
                        <td>{{ $user->email }}</td>
                        <td>{{ $role->name }} | {{ $role->label }}</td>
                        <td>
                            <form action="{{ route('levels.destroy'  ,$user->id) }}" method="post">
                                @method('DELETE')
                                @csrf

                                <div class="btn-group btn-group-xs">
                                    <a href="{{ route('levels.edit' ,$user->id) }}"  class="btn btn-primary">Edit</a>
                                    <button type="submit" class="btn btn-danger">Delete</button>
                                </div>
                            </form>
                        </td>
                    </tr>
                @endforeach
            @endif
        @endforeach
    </table>
</div>  
    

そして、結果は完全に表示されます:

ここに画像の説明を入力

しかし、今では、両方のパラメーターとして指定した [編集] ボタンと[削除]ボタンで問題が発生しました。$user->id

ボタンの上にカーソルを置くと、ユーザー ID が適切に定義されていることがわかります。

ここに画像の説明を入力

しかし、Route Model Binding を使用している edit メソッドになると、ユーザーが見つかりません。

public function edit(User $user)
    {
        dd($user->id); // return null
    }

ただし、Route Model Binding を使用せず、代わりに次のように言うと:

public function edit($id)
        {
            dd($id); // return 1
        }

ユーザーIDを正しく表示します!

ルート モデル バインディングがここで機能しない理由がわかりません。何が問題なのか、またはこの問題を解決する方法を知っている場合は、お知らせください...

4

2 に答える 2