laravel 在laravel中同时插入一个有很多关系表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24495564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:43:45  来源:igfitidea点击:

Inserting a has many relationship tables at the same time in laravel

phplaravellaravel-4eloquent

提问by Vishal Nair

I am using PHP Laravel Framework.I have 1 to M relationship tables orderand products.

我正在使用 PHP Laravel 框架。我有 1 到 M 个关系表orderproducts.

ORDER TABLE - orderid <--autoincrement primary key
                          - orderdate
                          - Name

ORDER TABLE - orderid <--autoincrement primary key
                          - orderdate
                          - Name

PRODUCTS TABLE - productid <--autoincrement primary key
                              - orderid <--foreign key reference to order table
                              - productName
                              - price
My models are as below -->
Order Model :

PRODUCTS TABLE - productid <--autoincrement primary key
                              - orderid <--foreign key reference to order table
                              - productName
                              - price
我的模型如下 -->
订购模型:

class Order extends Eloquent 
{

    protected $table = 'order';

    public function products()
    {
        return $this->hasMany('Products','orderid');
    }

}

Products Model :

产品型号:

class Products extends Eloquent 
{
    protected $table = 'products';
}

I have a form where I am taking the order date,name of the customer and the products details that can be more than one products.User can have one or many products in one order.
So now i have to insert details about both the tables in one go .
I read this doc http://laravel.com/docs/eloquent#inserting-related-models
they have given this below code for inserting related models -->

我有一个表格,其中包含订单日期、客户姓名和可以是多个产品的产品详细信息。用户可以在一个订单中拥有一个或多个产品。
所以现在我必须一次性插入有关两个表的详细信息。
我阅读了这个文档http://laravel.com/docs/eloquent#inserting-related-models
他们在下面给出了插入相关模型的代码-->

$comments = array(
    new Comment(array('message' => 'A new comment.')),
    new Comment(array('message' => 'Another comment.')),
    new Comment(array('message' => 'The latest comment.'))
);

$post = Post::find(1);

$post->comments()->saveMany($comments);

But here they know the id to find from the parent table but in my case I have to insert details in the Order table and then the details about the products in the Products table for the order which was inserted just before . Problem is how to find the newly inserted orderid? I am using autoincrement for orderidfield in the Order table.

但在这里他们知道要从父表中找到的 id,但在我的情况下,我必须在 Order 表中插入详细信息,然后在 Products 表中插入有关产品的详细信息,以便在之前插入的订单。问题是如何找到新插入的 orderid?我正在为orderidOrder 表中的字段使用自动增量。

回答by Jarek Tkaczyk

Simply:

简单地:

$order = new Order;
... // assign some properties
$order->save();

$products = [new Product(...), new Product(...)];

$order->products()->saveMany($products);

And set the protected $primaryKeyon the models, like Zwacky already said.

protected $primaryKey在模型上设置,就像 Zwacky 已经说过的那样。

Now, I doubt it's the relation you want. That means every product exists only in the context of a single order. I think this should be many to many, but that's your call.

现在,我怀疑这是你想要的关系。这意味着每个产品仅存在于单个订单的上下文中。我认为这应该是多对多,但这是你的要求。

回答by zwacky

if you create model by model you shouldn't have any problems. if you save()the model, its primary key idproperty will be set with the last inserted id automatically. here some idea:

如果您按模型创建模型,则应该没有任何问题。如果你save()是模型,它的主键id属性将自动设置为最后插入的 id。这里有一些想法:

$products = [];
foreach (['product A', 'product B'] as $name) {
    $product = new Product;
    $product->name = $name;
    $product->price = 15;
    $product->save();
    // $product->id will be set after save()
    array_push($products, $product);
}

$someOrder->products()->saveMany($products);