1. In Laravel 6, when querying a list (converted to an array), the reason it must be converted to an array is that the current requirement is to assign extra fields to individual records during the traversal. 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. This caused an N + 1 problem. Checking the number of generated SQL queries revealed 79 queries, an extra increase of 25 queries (because with was used, it actually increased by 50 query SQLs). As shown in Figure 1.

3. Finally, I decided to query the list in Laravel 6 (without converting to an array), and during the traversal, convert the individual object to 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. Checking the number of generated SQL queries revealed 22 queries. The 50 extra query SQLs that were actually added before have now been eliminated. This meets expectations. As shown in Figure 2.

