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
- Open a project
- Rename,
.env.example
as.env
- Install/reload packages (
composer install
) - Run database migrations and generate keys
- Run the project (
php artisan serve
)
composer install
cp .env.example .env
php artisan migrate
php artisan key:generate
php artisan serve
The following projects dive into the basics and customization of Laravel.
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. Namely, 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.
File | Description |
---|---|
laravel-terminology.md | New to Laravel? Check out the doc to understand the folder structures and terminology used. |
install-guide.md | Quick-start guide to installing PHP, Laravel, Xdebug, Composer, etc. |
debugging.md | How to debug your project using VS Code basics |
The following commands are used to clear the cache
php artisan cache:clear
php artisan route:clear
php artisan config:clear
php artisan view:clear
For a deeper clean, delete the folder, /vendor/
(60 MB).
After doing so, you will need to execute composer install
to reload the packages.
In the PascalCase sample projects the following mottos are applied:
The projects using PascalCase are an example of overriding Laravel's default naming conventions. In reality, most organizations have their own (legacy) conventions. As an example, variable names such as it be
passwd
vs.password
,userName
vs.name
, orrememberToken
instead ofremember_token
.Most samples people provide are a 1-to-1 on the naming, leaning into the Laravel "magic glue".
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.
# Create blank project
composer create-project laravel/laravel MyNewApp
# Create project using specific version "11.*" or "11.6"
composer create-project laravel/laravel SampleLaravel11App 11.*
# Add DB Migration Script (replace, "product", with your name)
# As a former DBA plural makes me cringe, Laravel likes plural like, "products" instead of "product" ):
php artisan make:migration create_product_table --create=product
# Run DB Migrations
php artisan migrate
# Create new Form Validation class
# Path: Path: app/Http/Requests/ProductStoreRequest.php
php artisan make:request ProductStoreRequest
# Create Controller and Model classes
artisan make:controller ProductController --resource --model=Product`
Before running a sample project please note the following
- 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.
- Copy
- Run with VS Code and the suggested Extensions
- See,
.vscode/extensions.json
for more info.
- See,
- Execute,
composer install
to download the Vendor packages. - Execute,
php artisan migrate
to create the database before runningphp artisan migrate:fresh
- Drop tables and recreatephp artisan migrate:fresh --seed
- Seed table with test data (when project states to do so)
- Launch project,
php artisan serve
- Template Projects
- VS Code Project Template
- Custom Database Naming Convention Template
- CRUD - Basic webpage form with Create, Read, Update, and Delete operations
- Form Validation - Form submission validator with recall of previously entered values
- REST API Login and CRUD operation - Including VS Code tester using REST Client extension
- REST API and CRUD - Slightly more complex implementation of the same thing
- Custom DB Naming Conventions - Column names using PascalCase and not the gross
snake_case
.- Pascal Case - Seeder - DB Factory and Seeder example using your PascalCase columns
- Pascal Case - API - Web API for creating "Customers and Products"
- Pascal Case - Filter Results
- Pascal Case - Include Invoice
- Pascal Case - POST PUT PATCH - Create and Update items
- Pascal Case - Upload Update - Massive upload information to store in DB
- Pascal Case - Unit Testing - How to test your Custom DB Naming Conventions
- Pivot Table - COMING SOON
- Web API Service - COMING SOON - Use services to link both View and API logic.
- Database - Pivot Table
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
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.)
- 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
Checkout debugging.md for more information
php artisan migrate --pretend
- Show the migration SQL queries before they're ran
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
- Sanctum vs Passport (OAuth2)
- Sanctum - Lightweight, single page apps, mobile apps, token based APIs.
- Passport - OAuth2
- Installing:
- Sanctum:
php artisan install:api
- Passport:
php artisan install:api --passport
- Sanctum:
- Sail with Docker
- LiveWire (GitHub)
- Database: Query Builder
- 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 - Blog, Video