2016-08-16 79 views
0

当我尝试新的记录添加到数据库Laravel返回错误:Laravel插入记录(外键)

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (kurwa_magazyn . transmits , CONSTRAINT transmits_to_user_id_foreign FOREIGN KEY (to_user_id) REFERENCES users (id)) (SQL: insert into transmits (from_user_id , to_user_id , product_id , quantity , transmit , created_at , updated_at , user_id) values (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'=>['[email protected]']]) !!} 


       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

可能重复的[Laravel 5:完整性约束违规:1452无法添加或更新子行:外键约束失败](http://stackoverflow.com/questions/29189311/laravel-5-integrity-constraint-违反-1452-不能添加或更新子行) – Sachith

+0

我希望这会帮助你。 http://stackoverflow.com/questions/29189311/laravel-5-integrity-constraint-violation-1452-cannot-add-or-update-a-child-row – Sachith

回答

0

好像当你试图

$transmit['to_user_id'] = $idKey2; 

你$ idKey像包含null或0

如果你想有一个可能性,设置空值,你应该让此列空。