kazweda / laravel-crud

0 stars 0 forks source link

削除機能(Delete) #9

Closed kazweda closed 1 day ago

kazweda commented 1 day ago

【Laravel】CRUD機能を作成する④削除機能(Delete)

kazweda commented 1 day ago

Screenshot 2024-09-20 at 5 15 57 PM

kazweda commented 1 day ago

削除ボタンを追加

@section('title', 'ユーザー一覧')

@section('content')

ユーザー一覧

<!-- ユーザー登録ページへのリンク -->
<a href="{{ route('users.create') }}"
    class="mb-4 inline-block bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
    ユーザー登録
</a>

<!-- セッションメッセージ -->
@if (session('message'))
    <div><strong>{{ session('message') }}</strong></div>
@endif

<div class="overflow-x-auto">
    <table class="min-w-full divide-y divide-gray-200">
        <thead>
            <tr>
                <th class="px-6 py-3 bg-gray-50 text-xs font-medium text-gray-500 uppercase tracking-wider">ID</th>
                <th class="px-6 py-3 bg-gray-50 text-xs font-medium text-gray-500 uppercase tracking-wider">名前</th>
                <th class="px-6 py-3 bg-gray-50 text-xs font-medium text-gray-500 uppercase tracking-wider">操作</th>
            </tr>
        </thead>
        <tbody class="bg-white divide-y divide-gray-200">
            @if (isset($users))
                @foreach ($users as $user)
                    <tr>
                        <td class="px-6 py-4 whitespace-nowrap">{{ $user->id }}</td>
                        <td class="px-6 py-4 whitespace-nowrap"><a href="{{ route('users.show', $user->id) }}"
                                class="text-blue-500 hover:underline">{{ $user->name }}</a></td>
                        <td class="px-6 py-4 whitespace-nowrap">
                            <a href="{{ route('users.edit', $user->id) }}"
                                class="text-indigo-600 hover:text-indigo-900">編集</a>
                            <form action="{{ route('users.destroy', $user->id) }}" method="POST" class="inline">
                                @csrf
                                @method('DELETE')
                                <button type="submit" class="text-red-600 hover:text-red-900 ml-20"
                                    onclick="return confirm('本当に削除しますか?')">削除</button>
                            </form>
                        </td>
                    </tr>
                @endforeach
            @endif
        </tbody>
    </table>
</div>

@endsection

kazweda commented 1 day ago

destroyメソッド

kazweda commented 1 day ago

ルートを追加