ARTICLE AD BOX
I'm building a Doctor management system in Laravel and I'm trying to implement a full CRUD (Create, Read, Update, Delete) using a Resource Controller and Blade views. I'm a beginner and want to make sure I'm structuring everything correctly.
Here is my current setup:
create.blade.php A form to add a new doctor with name, email, and specialization fields:
<x-app-layout> <x-slot name="header"> <h2 class="font-semibold text-xl text-gray-800 leading-tight"> {{ __('Add Doctor') }} </h2> </x-slot> <div class="py-12"> <div class="max-w-7xl mx-auto sm:px-6 lg:px-8"> <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg"> <div class="p-6 bg-white border-b border-gray-200"> <form method="POST" action="{{ route('doctors.store') }}"> @csrf <div class="mb-4"> <label for="name" class="block text-sm font-medium text-black"> {{ __('Name') }} </label> <input id="name" class="block mt-1 w-full" type="text" name="name" required autofocus /> </div> <div class="mb-4"> <label for="email" class="block text-sm font-medium text-black"> {{ __('Email') }} </label> <input id="email" class="block mt-1 w-full" type="email" name="email" required /> </div> <div class="mb-4"> <label for="specialization" class="block text-sm font-medium text-black"> {{ __('Specialization') }} </label> <input id="specialization" class="block mt-1 w-full" type="text" name="specialization" required /> </div> <div class="flex items-center justify-end mt-4"> <x-primary-button class="ml-4"> {{ __('Add') }} </x-primary-button> </div> </form> </div> </div> </div> </div> </x-app-layout>index.blade.php Lists all doctors in a table with Edit and Delete actions:
<x-app-layout> <x-slot name="header"> <h2 class="font-semibold text-xl text-gray-800 leading-tight"> {{ __('Doctors') }} </h2> </x-slot> <div class="py-12"> <div class="max-w-7xl mx-auto sm:px-6 lg:px-8"> <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg"> <div class="p-6 bg-white border-b border-gray-200"> <a href="{{ route('doctors.create') }}" class="mb-4 inline-block bg-blue-500 hover:bg-blue-700 text-black font-bold py-2 px-4 rounded"> Add Doctor </a> <table class="w-full border-collapse border border-gray-300"> <thead class="bg-gray-100"> <tr> <th class="border border-gray-300 px-4 py-2 text-left uppercase tracking-wider">Name</th> <th class="border border-gray-300 px-4 py-2 text-left uppercase tracking-wider">Email</th> <th class="border border-gray-300 px-4 py-2 text-left uppercase tracking-wider">Specialization</th> <th class="border border-gray-300 px-4 py-2 text-left uppercase tracking-wider">Actions</th> </tr> </thead> <tbody> @foreach ($doctors as $doctor) <tr class="hover:bg-gray-50"> <td class="border border-gray-300 px-4 py-2">{{ $doctor->name }}</td> <td class="border border-gray-300 px-4 py-2">{{ $doctor->email }}</td> <td class="border border-gray-300 px-4 py-2">{{ $doctor->specialization }}</td> <td class="border border-gray-300 px-4 py-2"> <a href="{{ route('doctors.edit', $doctor->id) }}" class="text-blue-500 hover:text-blue-700 mr-3">Edit</a> <form action="{{ route('doctors.destroy', $doctor->id) }}" method="POST" style="display:inline;"> @csrf @method('DELETE') <button type="submit" class="text-red-500 hover:text-red-700">Delete</button> </form> </td> </tr> @endforeach </tbody> </table> @if (session('success')) <div class="mb-4 text-green-600"> {{ session('success') }} </div> @endif @if (session('error')) <div class="mb-4 text-red-600"> {{ session('error') }} </div> @endif </div> </div> </div> </div> </x-app-layout>edit.blade.php A form to update an existing doctor's information:
<x-app-layout> <x-slot name="header"> <h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight"> {{ __('Edit Doctor') }} </h2> </x-slot> <div class="py-12"> <div class="max-w-7xl mx-auto sm:px-6 lg:px-8"> <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg"> <div class="p-6 bg-white border-b border-gray-200"> <form method="POST" action="{{ route('doctors.update', $doctor->id) }}"> @csrf @method('PUT') <div class="mb-4"> <label for="name" class="block text-sm font-medium text-gray-700 dark:text-gray-300"> {{ __('Name') }} </label> <input id="name" class="block mt-1 w-full" type="text" name="name" value="{{ $doctor->name }}" required autofocus /> </div> <div class="mb-4"> <label for="email" class="block text-sm font-medium text-gray-700 dark:text-gray-300"> {{ __('Email') }} </label> <input id="email" class="block mt-1 w-full" type="email" name="email" value="{{ $doctor->email }}" required /> </div> <div class="mb-4"> <label for="specialization" class="block text-sm font-medium text-gray-700 dark:text-gray-300"> {{ __('Specialization') }} </label> <input id="specialization" class="block mt-1 w-full" type="text" name="specialization" value="{{ $doctor->specialization }}" required /> </div> <div class="flex items-center justify-end mt-4"> <x-primary-button class="ml-4"> {{ __('Update') }} </x-primary-button> </div> </form> </div> </div> </div> </div> </x-app-layout>show.blade.php for viewing records
<x-app-layout> <x-slot name="header"> <h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight"> {{ __('Doctor Details') }} </h2> </x-slot> <div class="py-12"> <div class="max-w-7xl mx-auto sm:px-6 lg:px-8"> <div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg"> <div class="p-6 text-gray-900 dark:text-gray-100"> <h3 class="text-lg font-medium mb-4">Doctor Information</h3> <p><strong>Name:</strong> {{ $doctor->name }}</p> <p><strong>Specialization:</strong> {{ $doctor->specialization }}</p> <p><strong>Email:</strong> {{ $doctor->email }}</p> <p><strong>Phone:</strong> {{ $doctor->phone }}</p> </div> </div> </div> </div> </x-app-layout>My questions are:
Is this the correct way to structure Blade views for a Laravel Resource Controller (doctors.store, doctors.update, doctors.destroy)?
Where and how should I add form validation and display error messages in these Blade views?
Is using @method('PUT') and @method('DELETE') inside forms the right approach in Laravel?
What I have tried:
I followed the Laravel documentation on Resource Controllers and set up my routes using Route::resource('doctors', DoctorController::class);. The basic CRUD seems to work, but I'm unsure if I'm handling validation and error feedback correctly.
