Now that you have most of your application structure ready, it's time to interact with your database. But before doing that, you need to get a grasp of the tools Laravel uses to interact with a database. By default, Laravel uses an Object Relational Mapper (ORM). Learn why you should use an ORM, and why Eloquent is a great choice for your application.
- [Instructor] Frameworks were built upon the DRY premise, or Do not Repeat Yourself. And frameworks, just like design patterns, were created to solve issues that developers encounter, again and again. And database handling is not the exception. Laravel comes with an already-packaged and ready to use ORM, called Eloquent. But before getting into Eloquent, let's dip into what an ORM is. An ORM is an object-relational mapper, which is an abstraction of database table structure and actions.
The ORM helps us execute database actions without having to write SQL queries. It's also database-agnostic, that is, it will help us use different databases without us having to worry about the SQL flavor. Should we ever have the need to change the database engines, an ORM would help us do the switch seamlessly. There are different ORMs for different technologies. For example, ActiveRecord is the ORM that is normally used with Ruby on Rails.
And Symphony already comes with Doctrine out of the box. While it may not be suitable for small projects, Doctrine makes perfect sense for large projects, with its own DQL or Doctrine Query Language. Laravel is not the exception and comes with an ORM called Eloquent. While you may configure Laravel to use other ORMs, most examples and the documentation are heavily based on the assumption that we'll use Eloquent. So for now, let's use Eloquent.
Eloquent is an ORM based on the ActiveRecord design pattern. Named by Martin Fowler, the ActiveRecord pattern wraps a database table into a class, and an instance of that class to a single row. Any action performed on that object will be reflected on the database. Lastly, Foreign Keys relationships are exposed as separate instances. While Eloquent does have its own implementation of the ActiveRecord pattern, once you use it, you realize the astonishing resemblance with the ActiveRecord ORM for Rails.
Propel is another great choice for an ORM, which also implements the ActiveRecord pattern. It's simple to learn and, since it uses PDOs, it supports a large number of databases. RedBean PHP is another great way to go, as far as ORM selection is concerned. It employs an awesome strategy of zero configuration and it's great for prototyping. The downfall is that it may not be suitable for large projects that have already been designed.
- Laravel file and folder structure
- Dependency injection
- Routing requests to controllers
- Loading a view
- Creating a basic Blade template
- Sending data to a view
- Creating and validating forms
- Integrating a database
- Modifying data with Eloquent
- Returning data to a Laravel view
- Adding relational data and concurrent connections