gpt4 book ai didi

laravel - 使用 sync laravel 获取最后一个插入 ID

转载 作者:行者123 更新时间:2023-12-02 01:00:40 25 4
gpt4 key购买 nike

我想使用sync获取最后插入的id,这是我的代码

$id = $dealer->products()->sync($products);
$dealer_product->payment()->attach($id, $payment_data);

这是我的 DealerProduct 模型

class DealerProduct extends Eloquent {
use SoftDeletingTrait;
protected $guarded = array();

public function dealer() {
return $this->hasMany('Dealer');
}

public function product() {
return $this->belongsTo('Product');
}

public function items() {
return $this->belongsToMany('Item', 'dealer_products_has_items');
}

public function payment() {
return $this->belongsToMany('DealerProduct','dealer_product_payments','dealer_product_id');
}
}

这是我的 dealer_product_payments 表结构

id  int(10) 
dealer_product_id int(10)
paid_at date
expires_at date
note text

请帮帮我,谢谢

最佳答案

BelongsToMany::sync() 将返回这样的 id 数组

[
'attached' => [1, 2, 3],
'detached' => [4, 5],
'updated' => [6]
]

因此从该数组中,您知道附加了哪些 id

关于laravel - 使用 sync laravel 获取最后一个插入 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28958594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com