2016-11-09 1 views
0

메신저 테이블에 데이터를 삽입하려고하는데 메신저에 2 개의 외래 키가 있고 메신저에서이 오류가 발생했습니다. 무엇이 잘못하고 있는지 전혀 모른다.SQLSTATE [23000] : 무결성 제약 조건 위반 : 1452 자식 행을 추가하거나 업데이트 할 수 없습니다. 외래 키 제약 조건이 실패했습니다. laravel 5.2

이것은 내 주식 모델입니다.

//voorraad = stock 
// Model Voorraad has the attributes, Aantal and Id; 
// foreign keys are Producten_Id and Locaties_Id from the table Producten and locaties table 

class Voorraad extends Model 
{ 
    public $fillable = ['Id', 'aantal', 'Producten_Id', 'Locaties_Id']; 
    protected $table = 'Voorraad'; 

public $timestamps = false; 

public function producten() 
{ 
    return $this->BelongsTo('App\Producten', 'Producten_Id'); 
} 
public function locatie() 
{ 
    return $this->BelongsTo('App\Locatie', 'Locaties_Id'); 
} 
} 

이들은 데이터를 만들고 데이터베이스에 저장하는 데 사용하는 컨트롤러 기능입니다.

public function create() 
{  
    //retuning the view with database tables producten and locaties passing through to the create view the Id 

    return view('voorraad.create',[ 
     'producten' => Producten::all('Id'), 
     'locaties' => Locatie::all('Id') 
     ]); 
} 

public function store(Request $request) 
{ 

    //Producten_Id is the foreign key from the table producten 
    //Locaties_Id is the foreign key form the table Locaties 
    //aantal is the ammout of a sertain product 

    Voorraad::create($request->only(['aantal', 'Producten_Id', 'Locaties_Id'])); 



    return redirect(Route('voorraad.index')); 

} 

이 뷰

{!! Form::open(['url'=>'voorraad']) !!} 

{!! Form::label('aantal', 'aantal:') !!} 
{!! Form::text('aantal')!!} </br> 

<div class="form-group"> 
    {{ Form::label('producten_id', 'Producten_Id:') }} 
    {{ Form::Select('Producten_Id' , $producten, null) }}</br> 
</div> 

<div class="form-group"> 
    {{ Form::label('Locatie_Id', 'Id:') }} 
    {{ Form::select('Locaties_Id', $locaties, null) }} 
</div>  
    <div> 
     {!! Form::Submit('create', ['class' => 'btn btn-primary form-control']) !!} 
    </div> 
</div> 

를 생성한다 {! Form :: close() !!}

누군가 내가 잘못하고있는 것을 말해 줄 수 있다면 크게 감사하겠습니다. 내가 포함시키는 것을 잊어 버린 것이 있다면 알려주세요. 질문에 추가하겠습니다.

답변

0

우선 무엇보다 제가 변경하는 것이 좋습니다 -하지만 처음에는 그 상태에 대해 살펴 봅니다.

요청에서 추출하려고하는 데이터를 삭제하고 덤프하십시오. 정말로 기대하는 모든 데이터가 있는지 확인하십시오. 내 첫 번째 의도는 뭔가 잃어버린 것 같아요.

public function store(Request $request) 
{ 
    dd($request->only(['aantal', 'Producten_Id', 'Locaties_Id'])); 
    ... 
} 

이 문제를 해결할 수도 있지만 코드를 크게 변경하는 것이 좋습니다.

public $fillable = ['Id', 'aantal', 'Producten_Id', 'Locaties_Id']; 

결코 이드를 채울 수 없습니다. 사용자는 원하는대로 Id를 변경하거나 Id를 설정할 수 있습니다. 일반적으로 외래 키에 대해 동일한 작업을 수행합니다. 당신은 laravel 문서에서 언급 한 것처럼 그들을 연관시킵니다. https://laravel.com/docs/5.2/eloquent-relationships

$user->account()->associate($account); 

이드는 질량을 지정할 수 없습니다!

이러한 변경을 수행하면 외래 키 문제를 쉽게 해결할 수 있습니다. 당신이 $ p와 $ L을 보장 이로써

$p = Producten::findOrFail($request->input('Producten_Id')); 
$l = Locatie::findOrFail($request->input('Locaties_Id')); 

$v = new Voorraad(); 
$v->aantal = $request->input('aantal'); 
$v->locatie()->associate($l); 
$v->producten()->associate($p); 

$v->save(); 

그냥 빨리 예는 유효한 값이며, 그렇지 않으면 실패합니다.

관련 문제