Aviation Web Learning Lab
EN / ID
Theme

Module 2 - Day 4 of 5

Gemini CLI

Day 4: Views and Blade Templates

Welcome to Day 4! Yesterday you built the controller, routes, and two views (index and create). Your app can already list and create maintenance log entries. Today you will complete the remaining CRUD operations: viewing a single log entry in detail, editing an existing entry, updating it in the database, and deleting entries. You will also add form validation to prevent empty or incorrect data from being saved, and display error messages when validation fails. By the end of today, your app will have full CRUD functionality -- all four operations working correctly. This is like completing all the instruments on your panel!

Step 1

Build the show/detail page for a single maintenance log

Concept

When a technician needs to examine one specific maintenance log entry closely, they pull it from the records room. The show page does this digitally: it displays all the details of one single maintenance log entry. The show() method in your controller finds the specific entry by its ID and passes it to the view.

Apply

Write the show() method in your controller and create the show.blade.php view to display all fields of one maintenance log entry.

Prompt

I need to create a show/detail page for my maintenance logs. Please provide: 1. The show() method for MaintenanceLogController that uses MaintenanceLog::findOrFail($id) to get one log entry and returns view("maintenance-logs.show") 2. The complete Blade template for resources/views/maintenance-logs/show.blade.php that displays all fields: aircraft_id, date, maintenance_type, description, technician_name, status, created_at 3. Include a "Back to List" link and an "Edit" link on the page Show me both the controller method and the complete Blade template.

Expected Result

The show() method uses findOrFail() to retrieve one log by ID. The show.blade.php view displays all fields of that log entry with "Back to List" and "Edit" links. Visit http://localhost:8000/maintenance-logs/1 to see the detail page for log entry #1.

Troubleshooting

  • • If you see "404 Not Found" when visiting /maintenance-logs/1, make sure you have at least one log entry in the database. Use Tinker to create one if needed.
  • • If you see "Trying to get property of non-object", the $log variable might be null. Make sure findOrFail() is being used correctly and the ID exists in the database.
Step 2

Build the edit page with a pre-filled form

Concept

When an aircraft maintenance technician needs to correct a log entry in the hangar, they do not start from a blank form -- they pull up the existing entry and make changes to it. The edit page does exactly this: it displays the same form as the create page, but pre-filled with the existing data from the maintenance log. The student can change any field and then save the updated version.

Apply

Write the edit() method in your controller and create edit.blade.php with a form pre-filled with the existing log entry data. The form should use the PATCH method to update the record.

Prompt

I need to create an edit page for my maintenance logs. Please provide: 1. The edit() method for MaintenanceLogController that finds the log entry and returns view("maintenance-logs.edit") with the $log variable 2. The complete Blade template for resources/views/maintenance-logs/edit.blade.php with: - Same form fields as create.blade.php (aircraft_id, date, maintenance_type, description, technician_name, status) - All fields pre-filled with the existing $log data using value="{{ $log->field_name }}" - The status dropdown should show the current status as selected - @csrf and @method("PATCH") directives - Form action points to the update route for this specific log entry Show me both the controller method and the complete Blade template.

Expected Result

The edit() method returns the edit view with a $log variable. The edit.blade.php form is identical to create.blade.php but pre-filled with existing values. It includes @method("PATCH") for the update action. Visit /maintenance-logs/1/edit to see the pre-filled form.

Troubleshooting

  • • Remember to include @method("PATCH") in the edit form. HTML forms only support GET and POST, so Laravel uses @method to spoof PUT/PATCH/DELETE requests. Without it, the form will submit as POST and hit the store route instead of update.
  • • If the form fields are not pre-filled, make sure you are using value="{{ $log->field_name }}" for input fields and checking the selected state for the dropdown. For the description textarea, put {{ $log->description }} between the opening and closing tags.
Step 3

Build the update method to save edited entries

Concept

When an aircraft maintenance technician stamps a revision on an existing aircraft maintenance logbook entry, the old information is replaced with the corrected version. The update() method does this digitally: it validates the new data from the edit form, replaces the old data in the database with the corrected data, and redirects back to the detail page with a success message.

Apply

Write the update() method in MaintenanceLogController. It should validate the form data, update the existing log entry, and redirect to the show page.

Prompt

I need to write the update() method in my MaintenanceLogController. Please write code that: 1. Validates the request data with the same rules as store() (all required, status in:pending,in-progress,completed) 2. Finds the existing log entry using findOrFail($id) 3. Updates the entry with $log->update($validated) 4. Redirects to the show page for this log entry with a success flash message "Log entry updated successfully" Show me just the update() method code.

Expected Result

The update() method validates input, calls $log->update($validated) to save changes, and redirects to the show page with a success message. Try editing a log entry through the browser -- change the status from "pending" to "completed" and verify the change is saved.

Troubleshooting

  • • If the update does not save, check that the $fillable array in your MaintenanceLog model includes all the fields you are trying to update. Fields not in $fillable will be silently ignored.
  • • If you see "The PATCH method is not supported", make sure your edit form has @method("PATCH") right after @csrf.
Step 4

Build the destroy method to delete entries with confirmation

Concept

Removing an erroneous entry from the logbook is done carefully -- with confirmation, so the technician does not accidentally delete something important. The destroy() method deletes a log entry from the database, but only after the user confirms the deletion through a JavaScript dialog. This is like the careful process of removing an incorrect entry from a paper logbook -- you check twice before removing.

Apply

Write the destroy() method in your controller. Then add a delete button with JavaScript confirmation to your index and show views.

Prompt

I need to add delete functionality to my maintenance log app. Please provide: 1. The destroy() method for MaintenanceLogController that finds the log, deletes it with $log->delete(), and redirects to the index page with message "Log entry deleted" 2. A delete form that I should add to the Actions column in index.blade.php: - A small form with @csrf and @method("DELETE") - A submit button styled to look like a link that says "Delete" - onclick="return confirm('Are you sure you want to delete this log entry?')" on the button 3. The same delete form for show.blade.php Show me the controller method and the delete form HTML.

Expected Result

The destroy() method deletes the log entry and redirects to the index with a message. The index and show pages now have a "Delete" button/link that shows a confirmation dialog ("Are you sure you want to delete this log entry?") before actually deleting.

Troubleshooting

  • • If clicking Delete does nothing or shows a 404, check that @method("DELETE") is in the delete form and the form action URL is correct.
  • • The delete form MUST use @method("DELETE") because HTML forms do not support DELETE natively. Without it, the form submits as POST and will not reach the destroy method.
Step 5

Add form validation to store and update methods

Concept

Before a quality inspector accepts a maintenance log entry, they check that all required fields are filled in correctly -- no blank spaces, no invalid data, no impossible dates. Laravel's $request->validate() method acts as this quality inspector. It checks the form data against rules you define (required fields, maximum lengths, valid dates) and rejects the submission if anything is wrong. This prevents bad data from getting into your database.

Apply

Review your store() and update() methods to ensure they both have proper validation rules. Test by submitting an empty form to verify that validation prevents it.

Prompt

I have store() and update() methods in my MaintenanceLogController. Please review the validation rules and make sure both methods use $request->validate() with these rules: - aircraft_id: required, string, max:255 - date: required, date - maintenance_type: required, string, max:255 - description: required, string - technician_name: required, string, max:255 - status: required, string, in:pending,in-progress,completed Show me the complete validation code that should be in both methods. If my existing code already has this, just confirm it looks correct.

Expected Result

Both store() and update() should have $request->validate() with all 6 field rules. Test by submitting an empty create form -- the form should redirect back without saving. Test with an invalid status value (e.g., "unknown") -- it should also reject. This proves validation is working.

Troubleshooting

  • • If the form submits successfully even with empty fields, validation is not working. Make sure $request->validate() is called BEFORE MaintenanceLog::create(). The validated data should be stored in a $validated variable.
  • • Validation errors are flashed to the session automatically. If you submit invalid data and the form redirects back, that means validation is working correctly. You just cannot see the error messages yet -- you will add them in the next step.
Step 6

Add validation error display in Blade views

Concept

When a warning light appears on an aircraft instrument panel, it tells the pilot exactly which system needs attention and what is wrong. Validation error messages work the same way: they appear next to the form fields that have problems, telling the user exactly what needs to be fixed. Laravel makes this easy with the @error directive, which displays a red error message when a field fails validation.

Apply

Add @error blocks to both create.blade.php and edit.blade.php to display validation error messages below each form field.

Prompt

I need to add validation error messages to my create and edit Blade forms. Please show me how to add @error blocks after each form field. For each input field, I need: 1. An @error block that appears when validation fails 2. The error message should be displayed in red text below the field 3. Also show me how to display a success flash message at the top of the index page Show me the @error code pattern for one field (like aircraft_id) and the success message code. I will apply the same pattern to all fields.

Expected Result

Both create.blade.php and edit.blade.php should have @error blocks below each form field. When you submit an empty form, red error messages appear next to each empty required field. When you create or update a log entry successfully, a green success message appears at the top of the index page.

Troubleshooting

  • • If error messages do not appear, make sure you are using the @error directive correctly: @error('field_name') ... {{ $message }} ... @enderror. The field name must match exactly the validation rule key.
  • • If Blade syntax errors appear (like "Undefined variable" or unexpected @), check that every @error has a matching @enderror and all curly braces are balanced.

Safety Warning

Check Blade syntax carefully: every @foreach must have @endforeach, every @if must have @endif, every @error must have @enderror. Test your pages after every change. If changes do not appear, run "php artisan view:clear" to clear the view cache. Always keep a working git commit so you can undo mistakes.

Git Checkpoint

Run these commands to save your Day 4 progress: git add . git commit -m "Day 4: Blade views for show, edit, update, delete" Your app now has full CRUD functionality! All 4 operations work: Create, Read, Update, and Delete.

Self-Check

Quiz