Examples of my coding work and programming exercises completed during my learning journey.
Coding Examples
Mobile menu: keyboard and close behaviour
mobile-menu.js
export function initMobileMenu() {
const $menuToggle = $("#menu-toggle");
const $burger = $(".burger");
$(".main-nav a").on("click", function () {
closeMobileMenu($menuToggle);
});
$(document).on("keydown", function (event) {
handleEscapeKey(event, $menuToggle);
});
$burger.on("keydown", function (event) {
if (event.key === " " || event.key === "Enter") {
event.preventDefault();
$burger.trigger("click");
}
});
}
function closeMobileMenu($menuToggle) {
$menuToggle.prop("checked", false);
}
function handleEscapeKey(event, $menuToggle) {
if (event.key === "Escape" && $menuToggle.prop("checked")) {
closeMobileMenu($menuToggle);
}
}
Language
JavaScript (jQuery)
What it does
Closes the mobile menu when a link is clicked or Escape is pressed, and makes the burger button work with Space and Enter.
Why I used it
The menu is a CSS-only checkbox toggle, so JavaScript is only used for what CSS cannot do: keyboard accessibility and closing the menu after navigation. Small single-purpose functions keep the module easy to read.
Saves a validated enquiry into the enquiries table using a prepared statement. Optional fields such as company are stored as NULL when left empty.
Why I used it
Prepared statements with named placeholders keep user input separate from the SQL, which protects against SQL injection. Putting the query in its own function keeps the database logic out of the form handler and makes it reusable.
Company controller: updating a company
CompanyController.php
public function update(UpdateCompanyRequest $request, Company $company): RedirectResponse
{
$data = $request->safe()->except('logo');
if ($request->hasFile('logo')) {
if ($company->logo) {
Storage::disk('public')->delete($company->logo);
}
$data['logo'] = $request->file('logo')->store('logos', 'public');
}
$company->update($data);
return redirect()
->route('companies.show', ['company' => $company, 'page' => $request->query('page', 1)])
->with('status', 'Company updated.');
}
Language
PHP (Laravel)
What it does
Updates a company from validated form data. If a new logo is uploaded, the old file is deleted from storage and the new one is saved. If no file is sent, the current logo stays untouched.
Why I used it
The Form Request class handles validation before the controller runs, and safe()->except('logo') keeps the file out of the mass-assigned data, so the logo column is only changed when a new file really arrives. Deleting the old file prevents orphaned images from piling up in storage, and route model binding loads the company automatically.
More Coming Soon
This section will be expanded as I progress through the course.