You can reduce the amount of data by selecting only the columns you need: public function index() While doing this might seem reasonable (we are even paginating the results!), there may be a lot of information in our products' table that we don't need for a listing page (body, category id, product type, etc). 'products' => Products::query()->paginate() Our controller would look something like this: public function index() Let's say for example that we have an e-commerce website, and we want to display a list of products. The more data there is the more time it will take, but that's not all, all that data has to be stored in memory for the lifetime of the request and that can cause your server to slow down and run out of memory under heavy loads.įortunately, in Laravel, we can specify exactly the data we need. When you make a query, the data that the database returns is sent over the network. One way we can optimize a query is by reducing the amount of data we fetch from the database. Here I some techniques that can improve your load times in your Laravel application. Now, let's imagine we want to retrieve ActivityFeed instances and eager load the parentable parent models for each ActivityFeed instance.Did you know that 1 in 4 visitors would abandon a website if it takes more than 4 seconds to load? While multiple factors can slow down a website, one common factor is inefficient database queries.Įloquent is a great tool that makes it easy to interact with the database in our Laravel applications, but sometimes we forget that what looks like a method call or property is running a database query under the hood, which might lead to slow page loads or high memory usage. Additionally, let's assume that Photo models "have many" Tag models and Post models "have many" Comment models. We will assume the ActivityFeed model defines a "morph to" relationship named parentable that allows us to retrieve the parent Photo or Post model for a given ActivityFeed instance. In this example, let's assume that Photo and Post models may create ActivityFeed models. If you would like to eager load a "morph to" relationship, as well as related model counts for the various entities that may be returned by that relationship, you may utilize the with method in combination with the morphTo relationship's morphWithCount method. ![]() ![]() We can summarize the relationship's table structure like so:Ĭounting Related Models On Morph To Relationships In order to provide support for roles being assigned to multiple users, the role_user table is needed. This would mean that a role could only belong to a single user. Remember, since a role can belong to many users, we cannot simply place a user_id column on the roles table. This table is used as an intermediate table linking the users and roles. The role_user table is derived from the alphabetical order of the related model names and contains user_id and role_id columns. To define this relationship, three database tables are needed: users, roles, and role_user. So, a user has many roles and a role has many users. For example, a user may be assigned the role of "Author" and "Editor" however, those roles may also be assigned to other users as well. An example of a many-to-many relationship is a user that has many roles and those roles are also shared by other users in the application. Many-to-many relations are slightly more complicated than hasOne and hasMany relationships. Return $this -> throughEnvironments () -> hasDeployments () ![]() Eloquent makes managing and working with these relationships easy, and supports a variety of common relationships: For example, a blog post may have many comments or an order could be related to the user who placed it. Counting Related Models On Morph To Relationshipsĭatabase tables are often related to one another. ![]() Defining Custom Intermediate Table Models.Ordering Queries Via Intermediate Table Columns.Filtering Queries Via Intermediate Table Columns.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |