即時嘗試插入數據到我的股票表至少有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()!!}
如果有人能告訴我什麼即時做錯了,我將不勝感激。 如果有什麼我忘記包括只是讓我知道,我會將它添加到問題。