【问题标题】:How store data from a form into an exam table that contains foreign keys in laravel 5?如何将表单中的数据存储到包含 laravel 5 中外键的考试表中?
【发布时间】:2015-05-30 03:37:45
【问题描述】:

您好,我正在尝试将表单中的一些数据存储到我有三个外键的考试表中,我从像 {!! 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', 约束“gs_exam_idsess_foreign”外键(“idsess”)参考 'gs_session' ('idsess') ON DELETE CASCADE ON UPDATE CASCADE) (SQL: 插入 'gs_exam' () 值 ())

【问题讨论】:

  • 它给了你这个错误,因为在你的 sql 查询 SQL: insert into gs_exam() values ()) 中没有传递任何列,也没有可以插入的值。
  • 不,我说的是如何将数据插入到包含我的表单中的外键的表中
  • 可以通过Eloquent Model或者Query Builder插入

标签: forms laravel-5 php-5.6


【解决方案1】:

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

如果您的模型中有getIdattribute() 函数,请将其名称更改为getIdAttribute()(大写A)以进行约定,然后您可以简单地使用$user-&gt;id 访问该属性。与$user-&gt;getFullNameAttribute() 相同,只需使用$user-&gt;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

【讨论】:

    猜你喜欢
    • 2018-02-27
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 2018-04-16
    • 1970-01-01
    • 2021-01-10
    • 2021-12-25
    • 2014-01-14
    相关资源
    最近更新 更多