Skip to content

Commit

Permalink
Merge pull request #1790 from ulcuber/cache-has-related-table-in-hand…
Browse files Browse the repository at this point in the history
…le-blocks

Fixes n+1 queries for has related table in HandleBlocks
  • Loading branch information
haringsrob authored Oct 17, 2022
2 parents 76832b6 + 113579e commit 5693672
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Repositories/Behaviors/HandleBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

trait HandleBlocks
{
protected static $hasRelatedTableCache;

/**
* @param \A17\Twill\Models\Model $object
* @param array $fields
Expand Down Expand Up @@ -433,8 +435,7 @@ function ($files, $role) use ($locale, $block) {
protected function getBlockBrowsers($block)
{
return Collection::make($block['content']['browsers'])->mapWithKeys(function ($ids, $relation) use ($block) {
if (Schema::hasTable(config('twill.related_table', 'twill_related')) && $block->getRelated($relation)
->isNotEmpty()) {
if ($this->hasRelatedTable() && $block->getRelated($relation)->isNotEmpty()) {
$items = $this->getFormFieldsForRelatedBrowser($block, $relation);
foreach ($items as &$item) {
if (! isset($item['edit'])) {
Expand Down Expand Up @@ -491,4 +492,12 @@ protected function getBlockBrowsers($block)
];
})->filter()->toArray();
}

protected function hasRelatedTable(): bool
{
if (is_null(static::$hasRelatedTableCache)) {
static::$hasRelatedTableCache = Schema::hasTable(config('twill.related_table', 'twill_related'));
}
return static::$hasRelatedTableCache;
}
}

0 comments on commit 5693672

Please sign in to comment.