Book Image

Learning Laravel's Eloquent

By : Francesco Malatesta
Book Image

Learning Laravel's Eloquent

By: Francesco Malatesta

Overview of this book

<p>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.</p>
Table of Contents (16 chapters)
Learning Laravel's Eloquent
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

If you are associated with the field of web development, you know how important data is. The web runs on data, so it's essential for developers to think of quick and effective ways to deal with it. Eloquent is an awesome ORM that comes with the Laravel PHP framework. It is unique and is very beneficial to developers as it allows them to define models, relationships, and many complex operations with a really easy and intuitive syntax, without sacrificing performance. Performing an interesting number of operations on multiple tables without writing long queries with objects will be a bed of roses.

This book will take you through developing brilliant data-based applications with Eloquent, the Laravel framework ORM.

You will do the following:

  • Build highly efficient applications with the Eloquent ORM using an expressive syntax

  • Get to grips with the power of relationships and how Eloquent handles them

  • Go beyond simple theory with various step-by-step code examples

So, let's get started!

What this book covers

Chapter 1, Setting Up Our First Project, will discuss how to deal with Composer and Homestead. We will also cover the installation process of our very first Laravel project.

Chapter 2, Building the Database with the Schema Builder Class, will discuss the Schema Builder Class. We will analyze everything you can do with the class, look at different types of indexing, and learn about the methods that the Schema class provides.

Chapter 3, The Most Important Element – the Model!, will help us implement some "create," "read," "update," and "delete" logic for our items. We will also explore some useful methods and features of the model class.

Chapter 4, Exploring the World of Relationships, will help us discover how to work with different types of relationships and how to query and use them in a comfortable and clean way. Also, we will learn how to insert and delete related models in our database, or update existing ones.

Chapter 5, Using Collections to Enhance Results, will talk about collections. We will work with some results transformation methods and with the elements that make up a collection.

Chapter 6, Everything under Control with Events and Observers, will allow us to learn everything about the events in the context of Eloquent models. Right after, we will cover model events and model observers.

Chapter 7, Eloquent… without Laravel!, will explore the structure of the database package and see what is inside it. After that, we will learn how to install the "illuminate/database" package separately for your project and how to configure it for its first use. Yes, exactly: Eloquent without Laravel!

Chapter 8, It's Not Enough! Extending Eloquent, Advanced Concepts, will explore two different ways to extend Eloquent, and move on to learn about the Repository pattern.

What you need for this book

This book has no specific requests about hardware or software; we will use Vagrant to set up a standard virtual machine as a foundation for the example projects and code snippets. So, don't worry, my friend! No long afternoons or nights will be passed on the Apache or Nginx configuration files.

Who this book is for

This book is perfect for developers with a basic knowledge of PHP development, but who are new to the Eloquent ORM. However, developers with previous Laravel and Eloquent experience will also benefit from the in-depth analysis of specific classes and methodologies in the book.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "In the EventServiceProvider class, you can add a special event listener and bind it to a certain closure."

A block of code is set as follows:

  User::saved(function($user)
    {
        // doing something here, after User save operation (both create and update)...
    });

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "All you have to do is to go on the Download page of the Composer website and find the right method for your operating system."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.