0

嗨,我正在尝试将表单中的一些数据存储到我有三个外键的考试表中,我从这样的选择输入中获取这些外键{!! 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]:完整性约束违规:1452 无法添加或更新子行:外键约束失败('gesup'.'gs_exam',CONSTRAINT 'gs_exam_idsess_foreign'外键('idsess')参考'gs_session'('idsess ') ON DELETE CASCADE ON UPDATE CASCADE) (SQL: insert into 'gs_exam' () values ())

4

1 回答 1

0

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

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

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

于 2015-05-30T04:00:22.090 回答