Welcome to Day 3! You now have a database ready to store maintenance logs -- like a records room with empty logbooks waiting to be filled. Today you will build the system that lets users interact with those logs through a web browser. You will create a Controller (like an air traffic controller that directs incoming requests to the right place), define Routes (flight paths that map URLs to controller actions), build the index page to list all logs, create a form to add new logs, save new entries to the database, and display all logs in a table. By the end of today, your app will be able to create new maintenance log entries and display them. This is a big day -- you will see your app come to life!
Step 1
Create a resource controller for maintenance logs
Concept
An air traffic controller receives incoming aircraft and directs each one to the correct runway. A Laravel controller does the same thing for web requests: it receives a request (like "show me all maintenance logs" or "save this new log entry") and directs it to the correct action. A resource controller is a special type that comes with 7 pre-built methods for all CRUD operations: index, create, store, show, edit, update, and destroy.
Apply
In your terminal, run the Artisan command to create a resource controller called MaintenanceLogController.
Prompt
I need to create a resource controller for my maintenance logs in Laravel. Please give me the exact php artisan command to create a controller called MaintenanceLogController with all 7 resource methods (index, create, store, show, edit, update, destroy). Show me the exact command.
Expected Result
Your terminal should show "Controller created successfully." A new file app/Http/Controllers/MaintenanceLogController.php has been created with 7 empty methods: index, create, store, show, edit, update, and destroy. Each method has a comment describing its purpose.
Troubleshooting
• If you forget the --resource flag, the controller will be created without the CRUD methods. Delete the file and run the command again with --resource included.
• Make sure you type the controller name exactly as "MaintenanceLogController" (no spaces, PascalCase). Laravel uses this naming convention for controllers.
Step 2
Define routes in routes/web.php
Concept
Flight paths connect airports to runways -- they define which route an aircraft takes. Routes in Laravel do the same thing for web requests: they connect a URL (like /maintenance-logs) to a controller action (like the index method). Laravel's Route::resource creates all the flight paths you need for CRUD operations automatically -- one line of code creates 7 routes connecting URLs to your controller methods.
Apply
Open routes/web.php in your text editor. Add the import statement for your controller at the top, then add the Route::resource line to register all CRUD routes.
Prompt
I need to add a resource route for my MaintenanceLogController in the file routes/web.php. Please show me:
1. The "use" import statement I should add at the top of the file
2. The Route::resource line I should add to register all CRUD routes for maintenance-logs
Show me the exact two lines of code I need to add.
Expected Result
The routes/web.php file should have "use App\Http\Controllers\MaintenanceLogController;" at the top and "Route::resource('maintenance-logs', MaintenanceLogController::class);" in the routes section. You can verify by running "php artisan route:list" to see all registered routes.
Troubleshooting
• If you see "Target class [MaintenanceLogController] does not exist", check that the use import statement is correct and placed at the top of routes/web.php. Make sure there are no typos in the controller name.
• Run "php artisan route:list" to verify your routes are registered. You should see 7 routes for maintenance-logs covering GET, POST, PUT/PATCH, and DELETE methods.
Step 3
Build the index method to list all maintenance logs
Concept
The index page is like a clipboard showing all maintenance log entries at a glance -- the first thing a technician sees when checking the records. The index() controller method retrieves all maintenance log entries from the database and passes them to a view (HTML template) that displays them in a table format.
Apply
Open app/Http/Controllers/MaintenanceLogController.php and find the index() method. Write code to retrieve all maintenance logs and pass them to a view.
Prompt
I have a MaintenanceLogController with an empty index() method. Please write the code for the index() method that:
1. Retrieves all maintenance log entries using MaintenanceLog::all()
2. Returns a view called "maintenance-logs.index"
3. Passes the $logs variable to the view
Show me just the index() method code to paste into my controller.
Expected Result
The index() method in MaintenanceLogController should contain code that calls MaintenanceLog::all(), stores the result in a $logs variable, and returns view("maintenance-logs.index", compact("logs")). Also make sure "use App\Models\MaintenanceLog;" is imported at the top of the controller file.
Troubleshooting
• If you see "Class MaintenanceLog not found" in the controller, add "use App\Models\MaintenanceLog;" at the top of the controller file, after the namespace declaration.
• If the page shows "View [maintenance-logs.index] not found", that is expected -- you have not created the view file yet. You will create it in Step 6.
Step 4
Build the create method and create form view
Concept
The create page is like a blank maintenance log form -- an empty form ready to be filled in by the technician. The create() method in your controller simply displays the form. The form will have fields for aircraft_id, date, maintenance_type, description, technician_name, and status. When submitted, it sends the data to the store() method.
Apply
First, update the create() method in your controller to return the form view. Then create the Blade template file at resources/views/maintenance-logs/create.blade.php with an HTML form.
Prompt
I need two things for my Laravel create page:
1. The create() method code for MaintenanceLogController that returns view("maintenance-logs.create")
2. The complete Blade template for resources/views/maintenance-logs/create.blade.php with an HTML form that has:
- Input fields for: aircraft_id (text), date (date), maintenance_type (text), description (textarea), technician_name (text), status (select with options: pending, in-progress, completed)
- @csrf token for security
- A submit button labeled "Save Log Entry"
- Form action should submit to the store route
Show me both the controller method and the complete Blade template file.
Expected Result
The create() method returns view("maintenance-logs.create"). The file resources/views/maintenance-logs/create.blade.php exists with an HTML form containing all 6 fields, a @csrf directive, a select dropdown for status with 3 options, and a submit button.
Troubleshooting
• Make sure you create the folder resources/views/maintenance-logs/ first if it does not exist. Laravel expects Blade files in this exact path structure.
• Every form in Laravel MUST include @csrf inside the form tags. If you forget this, you will see a "419 Page Expired" error when submitting the form.
Step 5
Build the store method to save new log entries
Concept
When a technician fills in a maintenance log form and files it, the form goes into the records room for safekeeping. The store() method does the same thing digitally: it receives the form data from the create page, validates it, saves it to the database, and then redirects the user back to the index page with a success message.
Apply
Open MaintenanceLogController.php and write the store() method. It should validate the form data, create a new MaintenanceLog entry, and redirect to the index page with a success message.
Prompt
I need to write the store() method in my MaintenanceLogController. Please write code that:
1. Validates the request data (all fields required, aircraft_id and maintenance_type max 255 chars, date must be a valid date, description must be text, status must be one of: pending, in-progress, completed)
2. Creates a new MaintenanceLog entry using MaintenanceLog::create()
3. Redirects to the route "maintenance-logs.index" with a success flash message "Log entry created successfully"
Show me just the store() method code.
Expected Result
The store() method should validate all required fields using $request->validate(), create a new record using MaintenanceLog::create($validated), and redirect to the index route with a session flash message. If you submit the create form now, the data should be saved to the database.
Troubleshooting
• If you see a 419 "Page Expired" error when submitting the form, make sure @csrf is included inside the form in your create.blade.php template.
• If validation fails and you see a blank page or redirect back without an error message, that is normal Laravel behavior -- it redirects back to the form. You will add error message display in Day 4.
Step 6
Create the index Blade view to display all logs
Concept
The index view is like the instrument panel in a cockpit -- it displays all the important information at a glance. This view receives all maintenance log entries from the controller and displays them in a table with columns for key fields. It also has a link to the create page so users can add new entries.
Apply
Create the file resources/views/maintenance-logs/index.blade.php with a table that displays all maintenance logs and a link to create a new entry.
Prompt
Please write a complete Blade template for resources/views/maintenance-logs/index.blade.php. The template should:
1. Show a heading "Maintenance Logs"
2. Have a link to the create page labeled "Add New Log Entry"
3. Display a table with columns: Date, Aircraft, Type, Status, and Actions
4. Use @foreach to loop through $logs and display each entry's data
5. Show "No maintenance logs found" if the $logs collection is empty (use @empty or @forelse)
6. In the Actions column, show a "View" link for each entry
Use simple HTML -- no CSS needed yet. Show me the complete file content.
Expected Result
The file resources/views/maintenance-logs/index.blade.php exists with a table that displays maintenance log entries using @forelse/@empty, columns for date, aircraft_id, maintenance_type, and status, an "Add New Log Entry" link, and a "View" link for each row. Visit http://localhost:8000/maintenance-logs to see the page.
Troubleshooting
• If you see "Route [maintenance-logs.create] not defined", run "php artisan route:clear" to clear the route cache, then check that your Route::resource is in routes/web.php.
• If the table shows empty even though you have data, make sure the index() method passes $logs to the view correctly. Check that compact("logs") matches the variable name in @forelse($logs as $log).
Safety Warning
Review all generated controller code before using it. Make sure you understand what each route does before testing it in your browser. Never modify routes or controller methods you do not understand. If something unexpected happens, use git to restore your last working version.
Git Checkpoint
Run these commands to save your Day 3 progress:
git add .
git commit -m "Day 3: Controller and routes for maintenance logs"
Your app can now list and create maintenance log entries through the web browser!