There is no problem not worth solving, and no technology not worth learning!

Querying list (converted to array) in Laravel 6, you need to assign additional fields of a single record during the traversal process, and you need to use a single object, which in turn leads to the handling of n + 1 problems

导致 N + 1 问题的出现,查看生成的 SQL 数量,为 79 条,额外增加了 25 条查询(由于使用了 with,实际增加了 50 条查询 SQL)

1. Query the list (converted to an array) in Laravel 6, why you have to convert it to an array, the reason is that the current requirements are additional fields that need to be assigned a single record during the traversal process. This extra field does not exist in the model.


$wpThemes = Theme::select('id', 'custom_name', 'name', 'is_original', 'is_default', 'created_at_gmt', 'updated_at_gmt', 'original_theme_name')
            ->orderBy('updated_at_gmt', 'desc')
            ->get()->toArray();

foreach ($wpThemes as $wpTheme) {
	$wpThemeObject = $this->get($wpTheme['id']);
	$wpTheme['updatable'] = $this->isActionable($saasThemeConfig, $wpThemeObject, 'updatable');
}



2. Cause the occurrence of N + 1 problem, check the number of generated SQL, 79, and add 25 queries (because the use of with, actually add 50 query sql). as shown in Figure 1

导致 N + 1 问题的出现,查看生成的 SQL 数量,为 79 条,额外增加了 25 条查询(由于使用了 with,实际增加了 50 条查询 SQL)
Figure 1

3. The final decision is to query the list in Laravel 6 (not converted to an array), and during the traversal process, convert a single object into an array.


$wpThemes = Theme::select('id', 'custom_name', 'name', 'is_original', 'is_default', 'created_at_gmt', 'updated_at_gmt', 'original_theme_name')
            ->orderBy('updated_at_gmt', 'desc')
            ->get();

foreach ($wpThemes as $wpTheme) {
	$wpThemeArray = $wpTheme->toArray();
	$wpThemeObject = $this->get($wpThemeArray['id']);
	$wpThemeArray['updatable'] = $this->isActionable($saasThemeConfig, $wpTheme, 'updatable');
}



4. Check the number of generated SQL, it is 22, and 50 additional queries are actually added before, but now it has been canceled. in line with expectations. as shown in Figure 2

查看生成的 SQL 数量,为 22 条,之前实际增加了 50 条额外的查询 SQL,现在已经取消了。符合预期
Figure 2

PHP / Laravel / Yii2 Legacy Project Maintenance & Long-Term Technical Support

If your PHP / Laravel / Yii2 project is already in production but needs bug fixing, API troubleshooting, performance optimization, developer handover support, or long-term maintenance, feel free to contact me for remote technical support.

Ideal For:
✅ PHP legacy systems without active maintenance
✅ Laravel / Yii2 project bug fixes
✅ Admin panel feature iterations
✅ RESTful API troubleshooting
✅ MySQL / Redis / Nginx performance issues
✅ Long-term remote part-time maintenance

We can start with a small task:
✅ Production error troubleshooting
✅ API issue analysis
✅ Slow query and performance bottleneck diagnosis
✅ Initial code structure review
✅ Deployment environment and log inspection

If you would like to discuss your project, please contact me and mention: PHP Maintenance Consultation.

Contact Me:
Telegram: @shuijingwan
WeChat: 13980074657
Email: shuijingwanwq@gmail.com

评论

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.