Laravel很多人,如何在枢轴表中插入/获取数据?

发布于 2025-02-07 19:22:02 字数 575 浏览 4 评论 0原文

在Laravel 9中,我实施了许多与许多关系。

我有三个表,<代码> shop ,productproduct_shop表。

每个商店都可以具有每个产品的号码。 并且每种产品都可以存在于多家商店中。

问题是如何在product_shop表中插入/获取号码

我必须使用什么关系语法?

product_shop表:

$table->unsignedInteger('product_id');
$table->unsignedInteger('shop_id');
$table->unsignedInteger('number');

$table->foreign('product_id')->references('id')->on('products');
$table->foreign('shop_id')->references('id')->on('shops');

谢谢

in laravel 9 I implement many to many relationship.

I have three table, shop , product and product_shop table.

each shop can have number of every single product.
and each product can exist in multiple shops.

the question is how can I insert/get the number of product inside the product_shop table?

what relationship syntax I have to use?

product_shop table :

$table->unsignedInteger('product_id');
$table->unsignedInteger('shop_id');
$table->unsignedInteger('number');

$table->foreign('product_id')->references('id')->on('products');
$table->foreign('shop_id')->references('id')->on('shops');

thanks

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

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

发布评论

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

评论(1

撩动你心 2025-02-14 19:22:02

您可以使用Pivot。例如,

$product = $shop->products()->withPivot(['number'])->first();
$number = $product->pivot->number;

如果您想插入,

$shop->products()->attach([$product_id => ['number' => $your_value]]);

You can use pivot. For example,

$product = $shop->products()->withPivot(['number'])->first();
$number = $product->pivot->number;

And if you want to insert,

$shop->products()->attach([$product_id => ['number' => $your_value]]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文