Laravel: Up & Running, 3rd Edition

Book description

What sets Laravel apart from other PHP web frameworks? Speed and simplicity, for starters. This rapid application development framework and its ecosystem of tools let you quickly build new sites and applications with clean, readable code. Fully updated to include Laravel 10, the third edition of this practical guide provides the definitive introduction to one of today's most popular web frameworks.

Matt Stauffer, a leading teacher and developer in the Laravel community, delivers a high-level overview and concrete examples to help experienced PHP web developers get started with this framework right away. This updated edition covers the entirely new auth and frontend tooling and other first-party tools introduced since the second edition.

Dive into features, including:

  • Blade, Laravel's powerful custom templating tool
  • Tools for gathering, validating, normalizing, and filtering user-provided data
  • The Eloquent ORM for working with application databases
  • The Illuminate request object and its role in the application lifecycle
  • PHPUnit, Mockery, and Dusk for testing your PHP code
  • Tools for writing JSON and RESTful APIs
  • Interfaces for filesystem access, sessions, cookies, caches, and search
  • Tools for implementing queues, jobs, events, and WebSocket event publishing
  • Specialty packages including Scout, Passport, Cashier, and more

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. What This Book Is About
    2. Who This Book Is For
    3. How This Book Is Structured
    4. About the Third Edition
    5. Conventions Used in This Book
    6. O’Reilly Online Learning
    7. How to Contact Us
    8. Acknowledgements
  2. 1. Why Laravel?
    1. Why Use a Framework?
      1. “I’ll Just Build It Myself”
      2. Consistency and Flexibility
    2. A Short History of Web and PHP Frameworks
      1. Ruby on Rails
      2. The Influx of PHP Frameworks
      3. The Good and the Bad of CodeIgniter
      4. Laravel 1, 2, and 3
      5. Laravel 4
      6. Laravel 5
      7. Laravel 6
      8. Laravel Versions in the New SemVer World (6+)
    3. What’s So Special About Laravel?
      1. The Philosophy of Laravel
      2. How Laravel Achieves Developer Happiness
      3. The Laravel Community
    4. How It Works
    5. Why Laravel?
  3. 2. Setting Up a Laravel Development Environment
    1. System Requirements
    2. Composer
    3. Local Development Environments
      1. Artisan Serve
      2. Laravel Sail
      3. Laravel Valet
      4. Laravel Herd
      5. Laravel Homestead
    4. Creating a New Laravel Project
      1. Installing Laravel with the Laravel Installer Tool
      2. Installing Laravel with Composer’s create-project Feature
      3. Installing Laravel with Sail
    5. Laravel’s Directory Structure
      1. The Folders
      2. The Loose Files
    6. Configuration
      1. The .env File
    7. Up and Running
    8. Testing
    9. TL;DR
  4. 3. Routing and Controllers
    1. A Quick Introduction to MVC, the HTTP Verbs, and REST
      1. What Is MVC?
      2. The HTTP Verbs
      3. What Is REST?
    2. Route Definitions
      1. Route Verbs
      2. Route Handling
      3. Route Parameters
      4. Route Names
    3. Route Groups
      1. Middleware
      2. Path Prefixes
      3. Subdomain Routing
      4. Name Prefixes
      5. Route Group Controllers
      6. Fallback Routes
    4. Signed Routes
      1. Signing a Route
      2. Modifying Routes to Allow Signed Links
    5. Views
      1. Returning Simple Routes Directly with Route::view()
      2. Using View Composers to Share Variables with Every View
    6. Controllers
      1. Getting User Input
      2. Injecting Dependencies into Controllers
      3. Resource Controllers
      4. API Resource Controllers
      5. Single Action Controllers
    7. Route Model Binding
      1. Implicit Route Model Binding
      2. Custom Route Model Binding
    8. Route Caching
    9. Form Method Spoofing
      1. HTTP Verbs in Laravel
      2. HTTP Method Spoofing in HTML Forms
    10. CSRF Protection
    11. Redirects
      1. redirect()->to()
      2. redirect()->route()
      3. redirect()->back()
      4. Other Redirect Methods
      5. redirect()->with()
    12. Aborting the Request
    13. Custom Responses
      1. response()->make()
      2. response()->json() and ->jsonp()
      3. response()->download(), ->streamDownload(), and ->file()
    14. Testing
    15. TL;DR
  5. 4. Blade Templating
    1. Echoing Data
    2. Control Structures
      1. Conditionals
      2. Loops
    3. Template Inheritance
      1. Defining Sections with @section/@show and @yield
      2. Including View Partials
      3. Using Components
      4. Using Stacks
    4. View Composers and Service Injection
      1. Binding Data to Views Using View Composers
      2. Blade Service Injection
    5. Custom Blade Directives
      1. Parameters in Custom Blade Directives
      2. Example: Using Custom Blade Directives for a Multitenant App
      3. Easier Custom Directives for “if” Statements
    6. Testing
    7. TL;DR
  6. 5. Databases and Eloquent
    1. Configuration
      1. Database Connections
      2. URL Configurations
      3. Other Database Configuration Options
    2. Migrations
      1. Defining Migrations
      2. Running Migrations
    3. Inspecting Your Database
    4. Seeding
      1. Creating a Seeder
      2. Model Factories
    5. Query Builder
      1. Basic Usage of the DB Facade
      2. Raw SQL
      3. Chaining with the Query Builder
      4. Transactions
    6. Introduction to Eloquent
      1. Creating and Defining Eloquent Models
      2. Retrieving Data with Eloquent
      3. Inserts and Updates with Eloquent
      4. Deleting with Eloquent
      5. Scopes
      6. Customizing Field Interactions with Accessors, Mutators, and Attribute Casting
      7. Eloquent Collections
      8. Eloquent Serialization
      9. Eloquent Relationships
    7. Eloquent Events
    8. Testing
    9. TL;DR
  7. 6. Frontend Components
    1. Laravel Starter Kits
      1. Laravel Breeze
      2. Laravel Jetstream
    2. Laravel’s Vite Configuration
      1. Bundling Files with Vite
      2. The Vite Dev Server
      3. Working with Static Assets and Vite
      4. Working with JavaScript Frameworks and Vite
      5. Using Environment Variables in Vite
    3. Pagination
      1. Paginating Database Results
      2. Manually Creating Paginators
    4. Message Bags
    5. String Helpers, Pluralization, and Localization
      1. The String Helpers and Pluralization
      2. Localization
    6. Testing
      1. Testing Message and Error Bags
      2. Translation and Localization
      3. Disabling Vite in Testing
    7. TL;DR
  8. 7. Collecting and Handling User Data
    1. Injecting a Request Object
      1. $request->all()
      2. $request->except() and ->only()
      3. $request->has() and ->missing()
      4. $request->whenHas()
      5. $request->filled()
      6. $request->whenFilled()
      7. $request->mergeIfMissing()
      8. $request->input()
      9. $request->method() and ->isMethod()
      10. $request->integer(), ->float(), ->string(), and ->enum()
      11. $request->dump() and ->dd()
      12. Array Input
      13. JSON Input (and $request->json())
    2. Route Data
      1. From Request
      2. From Route Parameters
    3. Uploaded Files
    4. Validation
      1. validate() on the Request Object
      2. More on Laravel’s Validation Rules
      3. Manual Validation
      4. Using Validated Data
      5. Custom Rule Objects
      6. Displaying Validation Error Messages
    5. Form Requests
      1. Creating a Form Request
      2. Using a Form Request
    6. Eloquent Model Mass Assignment
    7. {{ Versus {!!
    8. Testing
    9. TL;DR
  9. 8. Artisan and Tinker
    1. An Introduction to Artisan
    2. Basic Artisan Commands
      1. Options
      2. The Grouped Commands
    3. Writing Custom Artisan Commands
      1. A Sample Command
      2. Arguments and Options
      3. Using Input
      4. Prompts
      5. Output
      6. Writing Closure-Based Commands
    4. Calling Artisan Commands in Normal Code
    5. Tinker
    6. Laravel Dump Server
    7. Customizing Generator Stubs
    8. Testing
    9. TL;DR
  10. 9. User Authentication and Authorization
    1. The User Model and Migration
    2. Using the auth() Global Helper and the Auth Facade
    3. routes/auth.php, Auth Controllers, and Auth Actions
    4. Breeze and Jetstream’s Frontend Templates
    5. “Remember Me”
    6. Password Confirmation
    7. Manually Authenticating Users
    8. Manually Logging Out a User
      1. Invalidating Sessions on Other Devices
    9. Auth Middleware
    10. Email Verification
    11. Blade Authentication Directives
    12. Guards
      1. Changing the Default Guard
      2. Using Other Guards Without Changing the Default
      3. Adding a New Guard
      4. Closure Request Guards
      5. Creating a Custom User Provider
      6. Custom User Providers for Nonrelational Databases
    13. Auth Events
    14. Authorization and Roles
      1. Defining Authorization Rules
      2. The Gate Facade (and Injecting Gate)
      3. Resource Gates
      4. The Authorize Middleware
      5. Controller Authorization
      6. Checking the User Instance
      7. Blade Checks
      8. Intercepting Checks
      9. Policies
    15. Testing
    16. TL;DR
  11. 10. Requests, Responses, and Middleware
    1. Laravel’s Request Lifecycle
      1. Bootstrapping the Application
      2. Service Providers
    2. The Request Object
      1. Getting a Request Object in Laravel
      2. Getting Basic Information About a Request
    3. The Response Object
      1. Using and Creating Response Objects in Controllers
      2. Specialized Response Types
    4. Laravel and Middleware
      1. An Introduction to Middleware
      2. Creating Custom Middleware
      3. Binding Middleware
      4. Passing Parameters to Middleware
    5. Default Middleware
      1. Maintenance Mode
      2. Rate Limiting
      3. Trusted Proxies
      4. CORS
    6. Testing
    7. TL;DR
  12. 11. The Container
    1. A Quick Introduction to Dependency Injection
    2. Dependency Injection and Laravel
    3. The app() Global Helper
    4. How the Container Is Wired
    5. Binding Classes to the Container
      1. Binding to a Closure
      2. Binding to Singletons, Aliases, and Instances
      3. Binding a Concrete Instance to an Interface
      4. Contextual Binding
    6. Constructor Injection in Laravel Framework Files
    7. Method Injection
    8. Facades and the Container
      1. How Facades Work
      2. Real-Time Facades
    9. Service Providers
    10. Testing
    11. TL;DR
  13. 12. Testing
    1. Testing Basics
    2. Naming Tests
    3. The Testing Environment
    4. The Testing Traits
      1. RefreshDatabase
      2. DatabaseMigrations
      3. DatabaseTransactions
      4. WithoutMiddleware
    5. Simple Unit Tests
    6. Application Testing: How It Works
    7. HTTP Tests
      1. Testing Basic Pages with $this->get() and Other HTTP Calls
      2. Testing JSON APIs with $this->getJson() and Other JSON HTTP Calls
      3. Assertions Against $response
      4. Authenticating Responses
      5. A Few Other Customizations to Your HTTP Tests
      6. Handling Exceptions in Application Tests
      7. Debugging Responses
    8. Database Tests
      1. Asserting Against the Database
      2. Asserting Against Eloquent Models
      3. Using Model Factories in Tests
      4. Seeding in Tests
    9. Testing Other Laravel Systems
      1. Event Fakes
      2. Bus and Queue Fakes
      3. Mail Fakes
      4. Notification Fakes
      5. Storage Fakes
    10. Working with Time in Tests
    11. Mocking
      1. A Quick Introduction to Mocking
      2. A Quick Introduction to Mockery
      3. Faking Other Facades
    12. Testing Artisan Commands
    13. Parallel Testing
    14. Browser Tests
      1. Choosing a Tool
      2. Testing with Dusk
    15. Pest
    16. TL;DR
  14. 13. Writing APIs
    1. The Basics of REST-Like JSON APIs
    2. Controller Organization and JSON Returns
    3. Reading and Sending Headers
      1. Sending Response Headers in Laravel
      2. Reading Request Headers in Laravel
    4. Eloquent Pagination
    5. Sorting and Filtering
      1. Sorting Your API Results
      2. Filtering Your API Results
    6. Transforming Results
    7. API Resources
      1. Creating a Resource Class
      2. Resource Collections
      3. Nesting Relationships
      4. Using Pagination with API Resources
      5. Conditionally Applying Attributes
      6. More Customizations for API Resources
    8. API Authentication
      1. API Authentication with Sanctum
      2. API Authentication with Laravel Passport
    9. Customizing 404 Responses
      1. Triggering the Fallback Route
    10. Testing
      1. Testing Passport
    11. TL;DR
  15. 14. Storage and Retrieval
    1. Local and Cloud File Managers
      1. Configuring File Access
      2. Using the Storage Facade
      3. Adding Additional Flysystem Providers
    2. Basic File Uploads and Manipulation
    3. Simple File Downloads
    4. Sessions
      1. Accessing the Session
      2. Methods Available on Session Instances
      3. Flash Session Storage
    5. Cache
      1. Accessing the Cache
      2. Methods Available on Cache Instances
    6. Cookies
      1. Cookies in Laravel
      2. Accessing the Cookie Tools
    7. Logging
      1. When and Why to Use Logs
      2. Writing to the Logs
      3. Log Channels
    8. Full-Text Search with Laravel Scout
      1. Installing Scout
      2. Marking Your Model for Indexing
      3. Searching Your Index
      4. Queues and Scout
      5. Performing Operations Without Indexing
      6. Conditionally Indexing Models
      7. Manually Triggering Indexing via Code
      8. Manually Triggering Indexing via the CLI
    9. The HTTP Client
      1. Using the HTTP Facade
      2. Handling Errors and Timeouts and Checking Statuses
    10. Testing
      1. File Storage
      2. Session
      3. Cache
      4. Cookies
      5. Log
      6. Scout
      7. HTTP Client
    11. TL;DR
  16. 15. Mail and Notifications
    1. Mail
      1. Basic “Mailable” Mail Usage
      2. Mail Templates
      3. Methods Available in envelope()
      4. Attaching Files and Inlining Images
      5. Markdown Mailables
      6. Rendering Mailables to the Browser
      7. Queues
      8. Local Development
    2. Notifications
      1. Defining the via() Method for Your Notifiables
      2. Sending Notifications
      3. Queueing Notifications
      4. Out-of-the-Box Notification Types
    3. Testing
      1. Mail
      2. Notifications
    4. TL;DR
  17. 16. Queues, Jobs, Events, Broadcasting, and the Scheduler
    1. Queues
      1. Why Queues?
      2. Basic Queue Configuration
      3. Queued Jobs
      4. Running a Queue Worker
      5. Handling Errors
      6. Controlling the Queue
      7. Queues Supporting Other Functions
    2. Laravel Horizon
    3. Events
      1. Firing an Event
      2. Listening for an Event
    4. Broadcasting Events Over WebSockets, and Laravel Echo
      1. Configuration and Setup
      2. Broadcasting an Event
      3. Receiving the Message
      4. Advanced Broadcasting Tools
      5. Laravel Echo (the JavaScript Side)
    5. Scheduler
      1. Available Task Types
      2. Available Time Frames
      3. Defining Time Zones for Scheduled Commands
      4. Blocking and Overlap
      5. Handling Task Output
      6. Task Hooks
      7. Running the Scheduler in Local Development
    6. Testing
    7. TL;DR
  18. 17. Helpers and Collections
    1. Helpers
      1. Arrays
      2. Strings
      3. Application Paths
      4. URLs
      5. Miscellaneous
    2. Collections
      1. The Basics
      2. A Few Collection Operations
    3. TL;DR
  19. 18. The Laravel Ecosystem
    1. Tools Covered in This Book
      1. Valet
      2. Homestead
      3. Herd
      4. The Laravel Installer
      5. Dusk
      6. Passport
      7. Sail
      8. Sanctum
      9. Fortify
      10. Breeze
      11. Jetstream
      12. Horizon
      13. Echo
    2. Tools Not Covered in This Book
      1. Forge
      2. Vapor
      3. Envoyer
      4. Cashier
      5. Socialite
      6. Nova
      7. Spark
      8. Envoy
      9. Telescope
      10. Octane
      11. Pennant
      12. Folio
      13. Volt
      14. Pint
    3. Other Resources
  20. Glossary
  21. Index
  22. About the Author

Product information

  • Title: Laravel: Up & Running, 3rd Edition
  • Author(s): Matt Stauffer
  • Release date: September 2023
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098153267