当我尝试向数据库添加新记录时Laravel返回错误:
SQLSTATE [23000]:完整性约束违规:1452无法添加或 更新子行:外键约束失败 (
kurwa_magazyn
。transmits
,CONSTRAINTtransmits_to_user_id_foreign
FOREIGN KEY(to_user_id
)参考users
(id
))(SQL:插入transmits
(from_user_id
,to_user_id
,product_id
,quantity
,transmit
,created_at
,updated_at
,user_id
)值(1,0,1,3,0,2016-08-16 18:08:53, 2016-08-16 18:08:53,1))
$transmit = new transmit($request->all());
$transmit['from_user_id'] = $idKey;
$transmit['to_user_id'] = $idKey2;
$transmit['product_id'] = $downloadProductsId;
$transmit['quantity'] = $downloadIlosc;
$transmit['transmit'] = 0;
$transmit['created_at'] = $dateNow;
$transmit['updated_at'] = $dateNow;
Auth::user()->transmits()->save($transmit);
//Session::flash('status1', 'Artykuł został dodany poprawnie');
return redirect('warehouse');
但是当我尝试只添加一个值时,例如$transmit['to_user_id'] = $idKey2;
,那么一切正常:
$transmit = new transmit($request->all());
$transmit['to_user_id'] = $idKey2;
Auth::user()->transmits()->save($transmit);
return redirect('warehouse');
我的文件模型transmit.php:
protected $fillable = [
'id',
'user_id',
'from_user_id',
'to_user_id',
'product_id',
'quantity',
'transmit',
'created_at',
'updated_at',
];
public function user(){
return $this->belongsTo('App\User');
}
public function products(){
return $this->belongsTo('App\Product');
}
并且文件添加新记录(transferProductUser.php):
<div class="col-xs-4">
{!! Form::model(['method'=>'post','class'=>'form-horizontal','action'=>['TransmitsController@updateTransferProduct']]) !!}
Produkt:
<select class="form-control selectpicker show-tick" data-live-search="true" data-size="5" name="products" id="products" onchange="copy()">
<option selected disabled style="color: orange">Wybierz...</option>
@foreach( $listProductOfSelectedUser as $listProduct)
<option data-subtext="(
<?php
$sn = DB::table('products')->select('sn')->where('id', '=', $listProduct->product_id)->get();
$quantity = DB::table($name_user)->select('quantity')->where('product_id', '=', $listProduct->product_id)->get();
?>
@foreach($sn as $serial)
@if($serial->sn == '')
<i>~brak SN~</i>
@elseif($serial->sn)
{{ $serial->sn }}
@endif
@endforeach
@foreach($quantity as $ilosc)
) ilość: <b>{{ $ilosc->quantity }} szt.</b>
@endforeach
" value="{{ $listProduct->product_id }}">
<?php
$article_name = DB::table('products')->select('article_id')->where('id', '=', $listProduct->product_id)->get();
?>
@foreach ($article_name as $name)
{{ article::find($name->article_id)->article_name }}
@endforeach
</option>
@endforeach
</select>
<br />
Do:
<select name="stan2" id="stan2" class="form-control selectpicker show-tick" data-size="5" onchange="run2(event)">
<option selected disabled>Wybierz...</option>
<optgroup id="użytkownicy" value="użytkownicy" label="użytkownicy">
@foreach($userListName as $user)
<option name="użytkownicy" value="{{ $user->id }}">{{ $user->name }}</option>
@endforeach
</optgroup>
<optgroup id="magazyny" value="magazyny" label="magazyny">
@foreach($warehouseList as $warehouse)
<option name="magazyny" value="{{ $warehouse->id }}">{{ $warehouse->name_warehouse }}</option>
@endforeach
</optgroup>
</select>
<div class="checkbox">
<label>
<input type="checkbox" id="isAgeSelected"> Podaj ilość produktów do przeniesienia.
</label>
</div>
<input type="number" class='form-control', id="txtAge" name="count" id="txtAge" style="display:none" min='0', max="", placeholder='Podaj ilość produktow do przeniesienia...'>
<br />
{!! Form::text('stan_key', 'user,'.$userId, ['class' => 'form-control', 'id'=>'copy']) !!}
{!! Form::text('stan_key2', null, ['class' => 'form-control', 'id'=>'copy3']) !!}
{!! Form::text('idProduct', null, ['class' => 'form-control', 'id'=>'copy2']) !!}
{!! Form::submit('Tak, przenieś', ['class' => 'btn btn-primary']) !!}
{!! link_to('warehouse', $title = 'Anuluj', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
</div>
<script>
function run2(event) {
console.log(document.getElementById("użytkownicy").label);
var $id = document.getElementById("stan2").value;
var lable=event.target.options[event.target.selectedIndex].parentNode.label
if(lable == 'użytkownicy'){
document.getElementById("copy3").value = "user,"+$id;
} else if (lable == 'magazyny') {
document.getElementById("copy3").value = "warehouse,"+$id;
}
}
document.getElementById('products').onchange = function () {
document.getElementById('copy2').value = event.target.value
}
//wyswietlenie inputa z quantity
$('#isAgeSelected').click(function() {
$("#txtAge").toggle(this.checked);
});
</script>
为什么我不能一次添加所有值?
答案 0 :(得分:0)
似乎在你想要的时候
$transmit['to_user_id'] = $idKey2;
您的$ idKey包含null或0。
如果您希望有可能设置空值,则应将此列设为可为空。