如何将表单中的数据存储到包含外键 laravel 5 的考试 table 中?

How store data from a form into an exam table that contains foreign keys in laravel 5?

你好我正在尝试将一些数据从表单存储到考试 table 其中我有三个外键,我从 select 这样的输入中获取这些外键 {!! 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() !!}`

但我收到以下错误:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails ('gesup'.'gs_exam', CONSTRAINT 'gs_exam_idsess_foreign' FOREIGN KEY ('idsess') REFERENCES 'gs_session' ('idsess') ON DELETE CASCADE ON UPDATE CASCADE) (SQL: insert into 'gs_exam' () values ())

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

如果您的模型中有一个 getIdattribute() 函数,请将其名称更改为 getIdAttribute()(大写 A)以符合约定,然后您只需使用 $user->id 即可访问该属性。与$user->getFullNameAttribute()相同,只需使用$user->fullName。这称为访问器:http://laravel.com/docs/5.0/eloquent#accessors-and-mutators

此外,如果您需要代码方面的帮助,我们需要了解更多信息。您没有显示正在调用(存储)的控制器功能,您是如何构建模型的,也没有显示您是如何保存它的。

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

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 方法将其 link 到您拥有的模型。

如何建立模型之间的关系: http://laravel.com/docs/5.0/eloquent#relationships 如何保存关系: http://laravel.com/docs/5.0/eloquent#inserting-related-models