Laravel Eloquent:
At the 14th Laravel Meetup Germany, the focus was on Object Rational Mapping (ORM). The use of Laravel Eloquent makes it easier to work with databases - without SQL.
Our byte5 newcomer Marlon showed the participants of the 14th Laravel Meetup Germany some tricks to make working in databases with Laravel Eloquent easier. The following code examples show how easy it is to optimise programmes with Eloquent. Where functions are used particularly often.
Eager Loading allows data to be loaded much more efficiently than when using Lazy Loading.
© byte5
Eager Load vs. Lazy Load
This code is to retrieve all posts from a database and output the respective title and author name. All posts are retrieved with all and the user model (author) of the post is reloaded in each foreach cycle. This leads to an unnecessarily large number of database queries. A faster alternative is to use with. Here, all posts are also retrieved, but the user models (authors) are only loaded once and with only one database query.
© byte5
Datenbankabfrage mit Lazy-Loading
© byte5
Datenbankabfrage mit Eager-Loading
The following example shows how a single post in a database can be found using a slug.
© byte5
where-Abfrage mit Slugs
In a conventional where query, the first parameter describes the database column to be searched. Parameter two defines the content to be searched for. However, the whole thing can be shortened by using Slug: Laravel automatically generates new where functions for the created database columns.
© byte5
Produktabfrage mit where
The aim of this example is to retrieve all active products from the database whose price is 250 or less. A conventional where query retrieves the products. What basically works well, however, can quickly become confusing as soon as the same query is carried out in several files. If you want to change the price at a later time, you have to change it manually in each file. This query can be made somewhat easier with the help of scopes. In order to be able to process the where query in a central place, the scopes must be added to the products model. Please note that the names of the scope functions always begin with "scope".
© byte5
Produktabfrage mit scopes
© byte5
Abfrage aller Datenbankeinträge vom Mai 2021
This example aims to load 50 posts from the database that were created in May 2021 and output them. With a conventional query, the posts are loaded from the database and the creation date is narrowed down with two where queries. This can quickly become confusing. Logical errors can also easily creep in. A more readable and error-free alternative is the same query with whereMonth and whereYear. With these two functions, the results can be narrowed down simply by specifying the month or year.
With the help of these easy-to-use examples, it is possible to make programmes in Eloquent more efficient and less prone to errors.
As already announced in the last Meetup, we would like to bring together the Laravel community in the German-speaking region in the medium term. Therefore, there will be no more Laravel Meetups Germany as from this summer. Instead, we will bundle the existing regional groups as Laravel DACH in the future. For you, this means: diverse topics, high quality and a continuous development of the community in Germany, Austria and Switzerland.
But don't worry, you won't have to do without the popular on-site meetings with pizza and beer at our Konfi in the future. As soon as current developments allow it again, we will switch to a hybrid model: We are planning a combination of talks on site in Frankfurt and speakers from other locations. This will also maintain the personal exchange in the Rhine-Main community. The next Laravel Meetup on the topic of Best Practices will take place on 8 July from 18:30 to 20:00. Here you can register for free: LINK!
Do you have any questions for our Laravel experts, suggestions for talk topics or would you like to take part in a Meetup as a speaker yourself? We look forward to hearing from you at laravel@byte5.de
Content & PR Management
Lea Auerbach
Join the
Laravel Meetup!
Register now for free!