![](/img/trans.png)
[英]Select2 keep showing "Searching" in ajax with pivot (n-n) in Laravel Backpack 5 Pro
[英]Optimize query in Laravel Backpack n-n relationships
我正在使用Laravel Backpack為網站構建后端面板。 確實很好,但是我注意到關系查詢非常昂貴。
我有兩個模型: 產品模型和中心模型,它們之間具有多對多關系。 在我的CenterCrudController中,我以這種方式定義了一個字段:
$this->crud->addColumns([
// More fields...
[
'label' => 'Products',
'type' => 'select2_multiple',
'name' => 'products', // the method that defines the relationship in your Model
'entity' => 'products', // the method that defines the relationship in your Model
'attribute' => 'name', // foreign key attribute that is shown to user
'model' => 'App\Models\Product', // foreign key model
'pivot' => true, // on create&update, do you need to add/delete pivot table entries?
],
// More fields...
]);
它工作正常,顯示了具有相關模型的選擇多個字段。 但是使用的查詢是SELECT * FROM products
,這是非常昂貴的(表產品具有成千上萬條帶有25列的記錄)。
在此示例中,我僅需要id和name字段。 我正在尋找類似查詢生成器的select()
方法。
有沒有一種方法可以優化這種類型的查詢?
提前致謝!
不知道這是否真的是答案,但是我還是會貼出來。
最好的解決方案(由@tabacitu指出)是使用select2_from_ajax field
。 僅當用戶單擊選擇字段時,它不會減慢頁面加載速度並發出ajax請求以獲取數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.