You are previewing Learning Laravel's Eloquent.
O'Reilly logo
Learning Laravel's Eloquent

Book Description

Develop amazing data-based applications with Eloquent, the Laravel framework ORM

In Detail

Learning Laravel's Eloquent starts off by taking you through setting up your first project and guiding you in creating a perfect Laravel environment. You will learn how to build the right database structure with the Migrations system and the Schema Builder class. Next, you will be introduced to the main element of Eloquent: the model. After treating the model as a single, isolated entity, you will learn how to create relations between them. You will be taken through organizing, filtering, and sorting your data with collections. You will then learn to enhance an application with new features using events and by creating new observers. Towards the end of the book, you will discover how to install, configure, and use the Eloquent ORM without Laravel. The book concludes by walking you through how to deal with complex problems and build advanced and flexible systems.

What You Will Learn

  • Configure a new Laravel project and set up database connections in minutes

  • Build and work with models to handle your data with an expressive syntax

  • Define relations between your models to create complex functionalities

  • Query your database using relations simply

  • Use collections to gather results and perform many operations on them with a powerful data structure

  • Use the Eloquent ORM without Laravel

  • Control your models during the request lifecycle with event listeners and observers

  • Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

    Table of Contents

    1. Learning Laravel's Eloquent
      1. Table of Contents
      2. Learning Laravel's Eloquent
      3. Credits
      4. About the Author
      5. Acknowledgments
      6. About the Reviewers
      7. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      8. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Errata
          2. Piracy
          3. Questions
      9. 1. Setting Up Our First Project
        1. Your Swiss Army Knife – Composer
          1. Installing Composer
          2. The composer.json and autoload files
          3. The most used commands
        2. Your safe place – Homestead
          1. Installing Homestead
            1. The Composer and PHP tool way
            2. The Git way
          2. Configuring Homestead
        3. The new hideout: Homestead improved
        4. A bonus tool – Adminer
        5. Your best friend: Laravel
          1. Installing Laravel
            1. Using the Laravel installer tool
            2. Using the Composer create-project command
        6. The first project – EloquentJourney
          1. The configuration system
          2. Setting up the database connection
        7. Summary
      10. 2. Building the Database with the Schema Builder Class
        1. The Schema Builder class
          1. Working with tables
          2. Working with columns
          3. Columns' methods reference
          4. Other $table object methods
          5. Updating tables and columns
          6. Indexes and foreign keys
        2. Database versioning with the migrations system
          1. Creating migrations
          2. Rolling back migrations
          3. More examples, more migrations
        3. Summary
      11. 3. The Most Important Element – the Model!
        1. Creating a Model
        2. Create, read, update, and delete operations basics
          1. Creating operations
          2. Reading operations
          3. Updating operations
          4. Deleting operations
        3. where, aggregates, and other utilities
          1. where and orWhere
            1. Magic wheres
          2. Aggregates
          3. Utility methods
        4. Mass assignment... for the masses
        5. Timestamps and soft deletes
          1. Timestamps
          2. Soft deleting
        6. Query scopes
        7. Attributes casting, accessors, and mutators
          1. Attributes casting
          2. Accessors and mutators
        8. Descending in the code
          1. A big file
          2. Quick conversion to array or JSON
          3. Imaginary attributes
          4. Route model binding
          5. Records chunking for memory optimization
        9. Summary
      12. 4. Exploring the World of Relationships
        1. The trinity – one to one, one to many, many to many
          1. One to one
            1. What exactly happened?
          2. One to many
          3. Many to many
          4. A question of inverses
        2. Querying-related models
          1. Accessing a pivot table
          2. Querying a relationship
        3. Eager loading (and the N + 1 problem)
          1. Basic eager loading
          2. Advanced eager loading
          3. Lazy eager loading
        4. Inserting and updating related models
          1. The save() and associate() methods
          2. What about many to many?
          3. The sync() method
        5. Accessing distant relationships
        6. More power – polymorphic relationships
          1. A simple polymorphic relationship
          2. A many-to-many polymorphic relationship
        7. Summary
      13. 5. Using Collections to Enhance Results
        1. Basic collection operations
        2. Transforming collections
        3. Iterating and filtering
          1. Iterating
          2. Filtering
        4. Sorting
        5. Summary
      14. 6. Everything under Control with Events and Observers
        1. When should I use events in my models?
        2. Model events
        3. An example of model events
        4. Events observers
        5. An example of model observers
        6. Summary
      15. 7. Eloquent… without Laravel!
        1. Exploring the directory structure
        2. Installing and configuring the database package
          1. Installing the package
          2. Configuring the package
        3. Using the ORM
        4. Using the Query and Schema Builder
          1. The Query Builder
          2. The Schema Builder
        5. Summary
      16. 8. It's Not Enough! Extending Eloquent, Advanced Concepts
        1. Extending the Model: Aweloquent!
          1. The Aweloquent Model
            1. Auto Hydrate
            2. Model self-validation
            3. Smart password hashing
            4. The autopurge of confirmation fields
          2. Extending the class
          3. The Auto Hydrate feature
          4. The Aweloquent Model self-validation feature – the basic version
          5. The Aweloquent Model self-validation feature – the operation-based version
          6. Smart password hashing and the confirmation fields autopurge method
          7. Fixing the save() Model method
        2. Diving into the repository pattern
          1. Hello, repository pattern!
          2. Introducing repositories – a concrete implementation
          3. Coding on Abstractions
          4. Repositories – a complete implementation
            1. Adding the new repository
        3. Summary
      17. Index