Duplicate queries

Hello, How can I get rid of duplicate queries? For example I have this query:
->relationship('product', 'description', function (Builder $query, Get $get) {
$query->where('brand_id', '=', $get('../../brand_id'));
})
->relationship('product', 'description', function (Builder $query, Get $get) {
$query->where('brand_id', '=', $get('../../brand_id'));
})
In reality each record has the same brand_id and this query should be executed once. However I have the feeling it gets called on each rows. Is there a way to have this only executed once?
1 Reply
JJSanders
JJSanders4mo ago
Some more background: I have an order. The order is for one brand only. In each orderline (=OrderItem) I want to be able to select a product which is related to the brand of the order. So Therefore I use this relationship code to select the products for the selected brands. Here is the full select:
Select::make('product_id')
->label('Product')
->hidden(function () use ($productId) {
return $productId !== null;
})
->searchable()
->debounce(600)
->getSearchResultsUsing(function (Get $get, string $search): array {
$brandId = $get('../../brand_id');
if (! $brandId) {
return ['Selecteer eerst een merk'];
}

return Product::select('id', DB::raw("CONCAT(IF(available, '', 'NML-'), item_code, ' - ', item_description) as concatenated_value"))
->where('brand_id', $brandId)
->having('concatenated_value', 'like', "%{$search}%")
->limit(25)
->pluck('concatenated_value', 'id')
->toArray();
})
->getOptionLabelUsing(function ($value): ?string {
$product = Product::find($value);
return $product ? $product->description : null;
})
->extraAttributes(fn($record) =>
$record?->product && !$record?->product?->available ? ['style' => 'background-color: #e58b8a;'] : []
)
->preload()
->disabled(fn (Get $get) => $get('../../sent'))
->columnSpan(2)
->required()
->relationship('product', 'description', function (Builder $query, Get $get) {
$query->where('brand_id', '=', $get('../../brand_id'));
})
->getOptionLabelFromRecordUsing(fn (Product $product): ?string => $product->description)
Select::make('product_id')
->label('Product')
->hidden(function () use ($productId) {
return $productId !== null;
})
->searchable()
->debounce(600)
->getSearchResultsUsing(function (Get $get, string $search): array {
$brandId = $get('../../brand_id');
if (! $brandId) {
return ['Selecteer eerst een merk'];
}

return Product::select('id', DB::raw("CONCAT(IF(available, '', 'NML-'), item_code, ' - ', item_description) as concatenated_value"))
->where('brand_id', $brandId)
->having('concatenated_value', 'like', "%{$search}%")
->limit(25)
->pluck('concatenated_value', 'id')
->toArray();
})
->getOptionLabelUsing(function ($value): ?string {
$product = Product::find($value);
return $product ? $product->description : null;
})
->extraAttributes(fn($record) =>
$record?->product && !$record?->product?->available ? ['style' => 'background-color: #e58b8a;'] : []
)
->preload()
->disabled(fn (Get $get) => $get('../../sent'))
->columnSpan(2)
->required()
->relationship('product', 'description', function (Builder $query, Get $get) {
$query->where('brand_id', '=', $get('../../brand_id'));
})
->getOptionLabelFromRecordUsing(fn (Product $product): ?string => $product->description)