Forms 如何将表单中的数据存储到包含laravel 5中外键的检查表中?

Forms 如何将表单中的数据存储到包含laravel 5中外键的检查表中?,forms,laravel-5,php-5.6,Forms,Laravel 5,Php 5.6,您好,我正在尝试将表单中的一些数据存储到一个检查表中,在这个表中我有三个外键,我从如下选择输入中获取这些外键:{!!form::open(['route'=>'exam.store','files'=>true])!! <div class="form-group"> {!! Form::label('session','Session :') !!} </div> <div class="form-group"> <select class

您好,我正在尝试将表单中的一些数据存储到一个检查表中,在这个表中我有三个外键,我从如下选择输入中获取这些外键:
{!!form::open(['route'=>'exam.store','files'=>true])!!

<div class="form-group">
   {!! Form::label('session','Session :') !!}
</div>
<div class="form-group">
   <select class="form-control" name="session">
       @foreach ($sessions as  $session)
           <option value='{{$session->idsess}}'>{{$session->libellesess}} </option>
       @endforeach
   </select>
</div>
<div class="form-group">

    {!! Form::label('prof','Enseignant :') !!}
</div>

    <div class="form-group">
    <select class="form-control" name="iden">
        @foreach ($users as   $user)
            <option value='{{ $user->getIdattribute()}}'>{{$user->getFullNameAttribute()}} </option>
        @endforeach
    </select>
        </div>

<div class="form-group">

{!! Form::label('date','Date:') !!}
    </div>
<div class="form-group">

    {!! Form::date('date',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
    {!! Form::label('heuredeb','Heure Début :') !!}
</div>
<div class="form-group">
    {!! Form::text('heuredeb',null,['class'=>'form-control','placeholder'=>'heure:minute']) !!}
</div>
<div class="form-group">
    {!! Form::label('heurefin','Heure Fin :') !!}
</div>

<div class="form-group">
    {!! Form::text('heurefin',null,['class'=>'form-control','placeholder'=>'heure:minute']) !!}
</div>


<div class="form-group">
    {!! Form::label('matiere','Matiere :') !!}
</div>
<div class="form-group">
    <select class="form-control" name="matiere">
        @foreach ($matieres as  $matiere)
            <option value='{{$matiere->idmat}}'>{{$matiere->libellemat}} </option>
        @endforeach
    </select>
</div>

<div class="form-group">
    {!! Form::label('statut','Statut :') !!}
</div>

<div class="form-group">
    {!! Form::text('statut',null,['class'=>'form-control','placeholder'=>'statut']) !!}`
</div>

{!! Form::submit('Ajouter',['class'=>'col-md-12 btn btn-danger'])!!}`
{!! Form::close() !!}`

{!!Form::label('session','session:')
@foreach($sessions作为$session)
{{$session->libelleses}
@endforeach
{!!Form::label('prof','Enseignant:')
@foreach($users作为$user)
{{$user->getFullNameAttribute()}
@endforeach
{!!Form::label('date','date:')
{!!Form::date('date',null,['class'=>'Form-control'])
{!!Form::label('heuredeb','heuredébut:')
{!!Form::text('heuredb',null,['class'=>'Form-control','placeholder'=>'heure:minute'])
{!!Form::label('heurefin','heurefin:')
{!!Form::text('heurefin',null,['class'=>'Form-control','placeholder'=>'heure:minute'])
{!!Form::label('matiere','matiere:')
@foreach($matieres as$matiere)
{{$matiere->libellemat}
@endforeach
{!!Form::label('statut','statut:')
{!!Form::text('statut',null,['class'=>'Form-control','placeholder'=>'statut'])`
{!!表单::提交('Ajouter',['class'=>'col-md-12 btn btn danger'])`
{!!Form::close()!!}`
但我得到了以下错误:

SQLSTATE[23000]:完整性约束冲突:1452无法添加或 更新子行:外键约束失败(“gesup”。“gs_检查”, 约束“gs\U考试\U IDSSS\U外键”外键('IDSSS')引用 更新级联上删除级联上的“gs_会话”(“idss”)(SQL: 插入“gs_考试”()值())


我在你的代码中看到了一些奇怪的东西

如果您的模型中有一个
getIdattribute()
函数,请将其名称更改为
getIdattribute()
(大写a)作为约定,然后您只需使用
$user->id
即可访问该属性。与
$user->getFullNameAttribute()
相同,只需使用
$user->fullName
。这称为访问器:

此外,如果您需要代码方面的帮助,我们需要了解更多。您没有显示正在调用的控制器函数(存储)、如何构造模型以及如何保存模型

无论如何,分配外键应该很容易,就像常规属性一样:

function yourControllerMethod(Request $request)
{
   $model = new MyModel();
   //assuming matiere_id is the foreign key to the Matiere table
   $model->matiere_id = $request->get('matiere');
   ... more stuff
   $model->save();
}
您还可以使用
associate
方法将其链接到您拥有的模型

如何在模型之间建立关系: 如何保存关系:

它给了您这个错误,因为在您的sql查询中,
sql:insert-into-gs\u-exam()值())
没有传递任何列,也没有可以插入的值。不,我说的是如何将数据插入到包含外键的表中,这些外键可以通过
雄辩模型
或使用
查询生成器