Introduction to Laravel Subquery
Laravel framework is perhaps the most sought-after framework among the PHP technologies, which follows MVC architecture. So, the Laravel subqueries are just querying nested inside another database query. When it seems quite difficult to retrieve the ancillary model data via a relationship, using subqueries in the Laravel database can be an effective method that does not need additional database queries.
What is Laravel Subquery?
It is something that contains more than one query and acts as a powerful tool in fetching data from multiple tables. Thus, generating a single result from various sources, making it easier for the coder to accumulate the fetched data as a single result.
As we are discussing subqueries, let me tell you one more thing about it. The statement which contains the query is called an outer query, whereas the question inside the report is what we call a subquery.
Eloquent Subquery
An Eloquent query is nothing but a queue within a query through which we can fetch information from more than one related table using only one single query. As only a single query can do your task so, the Eloquent query is quite helpful in limiting the number of queries. Hence, avoiding the database load.
Through our counter with various web developers and the internet, we have learned that people(especially junior developers) often find it challenging to get familiar with Laravel subqueries. It is seen in many cases that they do possess a handful of knowledge about various relational databases and are familiar with their commands and queries. But when it comes to using subqueries in Laravel, they often get stuck and confused. They lack the functionality and often fail to join the subqueries to the Laravel Eloquent query builder. However, we feel these problems arise due to a lack of a profound understanding of the subjects.
To become a good developer, you always need to keep two basic things in mind:
- Using minimum database queries.
- Using minimum data storage.
While most of the developers are less troubled by the first one but not all of them are pretty good at maintaining the second goal. So, we are going to set an example for Laravel 5 users on how to use a subquery in a SELECT statement using the Laravel query builder.
Here, we are using the function of DB:: raw() In SELECT statement, which can join our subquery to the “Eloquent Query Builder”.
In this case, we are using the tables of three different types as listed below:
- items
- items_stock
- items_sell
we intend to get the items, the sum of the stocks and record of each sale from the above charts.
1. Table data of items
2. Table data of items_stock
3. Table data of items_sell
Therefore, using MySQL queries seems relatively easy to most of the developers. However, everything from scratch here is the MySQL query for the above table:
Query:
SELECT
items.*,
(SELECT SUM(items_stock.stock) FROM items_stock
WHERE items_stock.product_id = items.id
GROUP BY items_stock.product_id) as item_stock,
(SELECT SUM(items_sell.sell) FROM items_sell
WHERE items_sell.product_id = items.id
GROUP BY items_sell.product_id) as item_sell
FROM `items`
Above, you can see the MySQL Query. Now, the fact about which we are talking about is that most of the programmers won’t find it hard to run the MySQL query, but they feel troubled when it comes to binding these queries to the Laravel Eloquent Query Builder. So, in Laravel 5 version, the above query will look like this:
$data = DB::table("items")
->select("items.*",
DB::raw("(SELECT SUM(items_stock.stock) FROM items_stock
WHERE items_stock.product_id = items.id
GROUP BY items_stock.product_id) as item_stock"),
DB::raw("(SELECT SUM(items_sell.sell) FROM items_sell
WHERE items_sell.product_id = items.id
GROUP BY items_sell.product_id) as item_sell"))
->get();
Conclusion
For producing and executing accurate queries, Laravel’s database query builder acts as a powerful yet convenient interface. Laravel’s query builder is comfortable with most other relational databases, which makes it possible for the developers to merge other databases’ queries with Laravel Query Builder. With all the other advantages which it possesses, it also contains an extra tool to safeguard your application against cyber attacks. It includes the PDO parameter binding to protect your web application from SQL injection attacks.
Recommended Article
We hope that this EDUCBA information on “Laravel Subquery” was beneficial to you. You can view EDUCBA’s recommended articles for more information.