Laravel与整数[]的类型PostgreSQL列的类型

发布于 2025-01-22 12:05:00 字数 510 浏览 1 评论 0 原文

我有类别表和产品表。在产品中表具有 category_id 列类型 integer []

ex:{1,2,3}

我需要产品带有类别关系 categories.id 存在 products.category_id

我在模型 > product

public function category()
{
    return $this->belongsTo(Category::class, \DB::raw("ANY(category_id)"), 'id');
}

否获取类别为nu​​ll

I have categories table and products table. in products table have category_id column type of integer[].

ex: {1,2,3}

.

And I need products list with category relation which categories.id exist products.category_id

I tried in model Product:

public function category()
{
    return $this->belongsTo(Category::class, \DB::raw("ANY(category_id)"), 'id');
}

no get category is null.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

护你周全 2025-01-29 12:05:00

您应该使用属于许多关系。
因为整数[]类型用于保存INT数组。
尝试在这样的模型中设置它:

在您的 product(Model)中,您将获得此关系方法:

public function categories(): BelongsToMany
{
    return $this->belongsToMany(Category::class);
}

以及在您的 category(model)中:

public function products(): BelongsToMany
{
    return $this->belongsToMany(Product::class);
}

refrence

you should use belongs to many relation.
because integer[] type is for saving arrays of ints.
try to set it in your model like this:

in your Product(model) you will get this relation method:

public function categories(): BelongsToMany
{
    return $this->belongsToMany(Category::class);
}

And in your Category(model):

public function products(): BelongsToMany
{
    return $this->belongsToMany(Product::class);
}

Refrence

一江春梦 2025-01-29 12:05:00

您可以使用Laravel查询构建器尝试一下

public function category()
{
 return DB::table('products')
 ->join('categories', 'products.category_id', '=', 'categories.id')
 ->get();
}

You can try this using laravel query builder

public function category()
{
 return DB::table('products')
 ->join('categories', 'products.category_id', '=', 'categories.id')
 ->get();
}
栀子花开つ 2025-01-29 12:05:00

首先,我认为不可能使用Laravel关系方法来做到这一点。

其次,如果您使用的是Postgres(这是一个关系数据库),则绝对应该阅读数据库归一化的基础知识。

我建议您有一个所谓的枢轴表,将您的产品链接到您的类别,看起来像这样的类别:

免责声明:您不需要为此创建模型。只需使用 PHP Artisan Make进行迁移:迁移create_categories_products_table

categories_products

|         id          |     category_id  |      product_id     |
|---------------------|------------------|---------------------|
|          55         |         1        |          5          |
|          56         |         2        |          5          |
|          57         |         3        |          5          |
|          58         |         1        |          6          |

此表链接您的表,这比存储的一些数组更容易处理。也许考虑一下,这并不是那么多。您可以在Laravel文档上阅读它,也可以尝试在Google上搜索枢轴表和归一化。

完成此操作时:

现在您可以使用Laravel allavel 这样的关系:

// Product.php
public function categories()
{
    return $this->belongsToMany(Category::class, 'categories_products');
}
// Category.php
public function products()
{
    return $this->belongsToMany(Product::class, 'categories_products');
}

First of all, I dont think it's possible to do this with the Laravel Relationship Methods.

Second of all, if you are using Postgres, which is a relational Database, you should definitely read up on the fundamentals of database normalization.

I would recommend you have a so called pivot table, that links your products to your categories, which could look something like this:

Disclaimer: You dont need to create a Model for this. Just make a migration with php artisan make:migration create_categories_products_table

categories_products

|         id          |     category_id  |      product_id     |
|---------------------|------------------|---------------------|
|          55         |         1        |          5          |
|          56         |         2        |          5          |
|          57         |         3        |          5          |
|          58         |         1        |          6          |

This table links your tables and this is much more easy to handle than some arrays stored as json.. maybe think about it, it is not that much work to do. You can read upon it on the Laravel Documentation or try searching on google for pivot tables and normalization.

When you have done that:

Now you can just use the Laravel belongsToMany Relationship like so:

// Product.php
public function categories()
{
    return $this->belongsToMany(Category::class, 'categories_products');
}
// Category.php
public function products()
{
    return $this->belongsToMany(Product::class, 'categories_products');
}
痴梦一场 2025-01-29 12:05:00

我无法关系,但是与属性我可以获得类别,

首先将类别施放到数组中,并且

public function getCategoriesAttribute()
{
    return Category::whereIn('id',$this->category_id)->get();
}

它有效

I can't relation but with attribute i can get categories

firstly cast category_id to array and

public function getCategoriesAttribute()
{
    return Category::whereIn('id',$this->category_id)->get();
}

and it works

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文