繁体   English   中英

优化Laravel Backpack nn关系中的查询

[英]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列的记录)。

在此示例中,我仅需要idname字段。 我正在寻找类似查询生成器的select()方法。

有没有一种方法可以优化这种类型的查询?

提前致谢!

不知道这是否真的是答案,但是我还是会贴出来。

最好的解决方案(由@tabacitu指出)是使用select2_from_ajax field 仅当用户单击选择字段时,它不会减慢页面加载速度并发出ajax请求以获取数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM