[英]how to add extra data with sync method laravel
我正在使用 Laravel 並且我有三個表:products、 features 和 feature_product 第三個表有一個額外的列“值”,我想在同步時添加每個功能的值
所以同步方法在沒有價值的情況下工作,但每當我想發送價值時,它都會給我錯誤
這是我的代碼
if($request->get('feature')){
foreach($request->get('feature') as $featureName)
{
$feature = Feature::find($featureName);
if($feature)
{
$featureIds[] = $feature->id;
}
}
$product->features()->sync($featureIds);
}
這是我的錯誤
一般錯誤:1364 字段“值”沒有默認值(SQL:插入到
feature_product
(feature_id
,product_id
) values (11, 99))
我知道這是因為我沒有為此發送價值,我想知道我該怎么做
您缺少同步的樞軸值,這就是您收到此錯誤的原因。
if($request->get('feature')){
$featureIds = array();
foreach($request->get('feature') as $featureName)
{
$feature = Feature::find($featureName);
if($feature){
$featureIds[$feature->id] = ['value'=>'yourvalue'];
}
}
$product->features()->sync($featureIds);
}
嘗試使這種數組同步。
$product->features()->sync( array(
1 => array( 'value' => 'xyz' ),
2 => array( 'value' => 'abc' ),
...
));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.