繁体   English   中英

关系1-n多个BACKPACK Laravel

[英]Relationship 1-n multiple BACKPACK Laravel

我在最后一个 laravel 上使用背包 5 Pro。

我想 select 几个数据,将它们保存在数据库中并检索它们。

我有两张桌子:

产品- ID、名称

文章- id, title, products_id

在我的 model Articles.php中:

public function produit()
{
    return $this->belongsTo('App\Models\Products', 'products_id', 'id');
}

在我的 controller ArticlesCrudController.php中:

$this->crud->addField(
    [
        'label'     => "Products Links",
        'type'      => 'relationship',
        'name'      => 'produit', 
        'entity'    => 'produit', 
        'model'     => "App\Models\Products",
        'attribute' => 'product_name', 
        'allows_null' => true,
        'multiple'     => true, <--- WANT MULTIPLE SELECT
        'tab' => 'Products links',
        'options'   => (function ($query) {
                    return $query->orderBy('product_name', 'ASC')->get();
        }),
    ]
);

在我的数据库中:

products_id包含:["15","18"]

除我的结果列表外,一切正常。 它只显示一个结果(id:15)而不是两个或更多...(15 和 18)。

列表结果

这不是建立这种关系的方式,您只能在product_id中存储一个值。

根据您的结构,您的产品可以有多个文章,因此在您的文章中创建两行(数据库中的两条记录):

考虑这个伪代码:

$article1 = [
   'id' => 1,
   'title' => 'Article 1',
   'product_id' => 15
];

$article2 = [
   'id' => 1,
   'title' => 'Article 1',
   'product_id' => 15
]

现在 ID 为 15 的产品有 2 篇文章(1 和 2)。

如果您希望您的文章有多个产品,反之亦然,则创建多对多关系

暂无
暂无

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

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