Skip to content

Learn how to use Laravel 11 through small example apps

Notifications You must be signed in to change notification settings

DamianSuess/Learn.Laravel

Repository files navigation

Learn Laravel 11

In this edition of the Suess Labs' Learn repository, we'll dive into the Laravel PHP framework for web applications.

Author: Damian Suess
Website: suesslabs.com

Overview

The following projects dive into the basics and customization of Laravel.

Foreword

Looking back, I've usually always created custom micro-frameworks with PHP to keep things lean, quick, and target a project's needs. However, there are a few bottlenecks that can come of that. Namingly, rapid prototyping, maintainability, and scalability can quickly pinch points throughout a product's lifecycle. As a consideration, frameworks like Laravel can assist with such things.

By all means, explore and build your frameworks! This will teach you a lot of solid fundamentals, especially the core functionality of PHP.

PascalCase

In the PascalCase sample project the following mottos are applied:

  1. The projects using PascalCase are an example of overriding Laravel's default naming conventions. In reality, most organizations have their own (legacy) conventions. Whether it be passwd vs. password, userName vs. name, or rememberToken instead of remember_token.

  2. Most samples people provide are a 1-to-1 on the naming, leaning into the Laravel "magic glue".

  3. A framework should be flexible and well-documented to suit the customer's needs. When it's too ridged, copious amounts of scaffolding and code smells will occur.

Reproduce Samples

composer install
cp .env.example .env
php artisan migrate
php artisan key:generate
php artisan serve

Extensions and More

Before running a sample project please note the following

  1. The .env files are not saved by default.
    • Copy .env.example as .env before running
    • Error 500 may occur when the .env file is missing.
  2. Run with VS Code and the suggested Extensions
    1. See, .vscode/extensions.json for more info.
  3. Execute, composer install to download the Vendor packages.
  4. Execute, php artisan migrate to create the database before running
    • php artisan migrate:fresh - Drop tables and recreate
    • php artisan migrate:fresh --seed - Seed table with test data (when project states to do so)
  5. Launch project, php artisan serve

Sample Projects

  1. Template Projects
    1. VS Code Project Template
    2. Custom Database Naming Convention Template
  2. CRUD - Basic webpage form with Create, Read, Update, and Delete operations
  3. Form Validation - Form submission validator with recall of previously entered values
  4. REST API Login and CRUD operation - Including VS Code tester using REST Client extension
  5. REST API and CRUD - Slightly more complex implementation of the same thing
  6. Custom DB Naming Conventions - Column names using PascalCase and not the gross snake_case.
    1. Pascal Case - Seeder - DB Factory and Seeder example using your PascalCase columns
    2. Pascal Case - API - Web API for creating "Customers and Products"
    3. Pascal Case - Filter Results
    4. Pascal Case - Include Invoice
    5. Pascal Case - POST PUT PATCH - Create and Update items
    6. Pascal Case - Upload Update - Massive upload information to store in DB
    7. Pascal Case - Unit Testing - How to test your Custom DB Naming Conventions
    8. Pivot Table - COMING SOON
  7. Web API Service - COMING SOON - Use services to link both View and API logic.
  8. Database - Pivot Table

Creating a Sample

composer create-project laravel/laravel #.#-AppName
composer require --dev friendsofphp/php-cs-fixer
./vendor/bin/php-cs-fixer fix       # Reformats EVERYTHING!
# ./vendor/bin/php-cs-fixer fix app # Reformats only /app/ folder

References

The projects listed here are based on the following examples. The examples in this repository have been modified and improved upon (bug fixes, added functionality, testability, etc.)

Coming Soon

  • Deleting Multiple Records
  • Publish API Route File
  • File Upload Example
  • AJAX Request Example
  • JQuery Ajax Loading Spinner Example
  • Generate and Read Sitemap XML File Example
  • Testing Database with Factory and Seeders

Debugging Tips

Checkout debugging.md for more information

  1. php artisan migrate --pretend
    1. Show the migration SQL queries before they're ran

Naming Conventions

There are many "rules" out there which veer off from one another, however, the following appears to be widely accepted. (Frankly, I often use Xeno Innovations' legacy rules.)

  • Models - Always singular and PascalCase
  • Controllers - Usually singular and ending with the word, Controller. i.e. PostController

Other References