以下是我的关系。
SubmitApplication.php
public function requisition()
{
return $this->hasMany(Requisition::class);
}
Requisition.php
public function submitApplication()
{
return $this->belongsTo(SubmitApplication::class);
}
我的刀片
<form class="btn-group" action="{{ route('requisitions.update', ['id' => $requisition->id]) }}" method="post">
{{ method_field('PATCH') }}
{{ csrf_field() }}
<button type="submit" class="btn btn-success btn-xs">تایید</button>
</form>
RequisitionController.php
public function update(Request $request, SubmitApplication $submitApplication)
{
$submitApplication->approved = 1;
$submitApplication->save();
return redirect()->back();
}
对于请购单表:
public function up()
{
Schema::create('requisitions', function (Blueprint $table) {
$table->increments('id');
$table->integer('school_id')->unsigned();
$table->string('type');
$table->string('status');
$table->string('date');
$table->timestamps();
$table->foreign('school_id')->references('id')->on('schools');
});
}
对于submit_applications表:
public function up()
{
Schema::create('submit_applications', function (Blueprint $table) {
$table->increments('id');
$table->integer('requisition_id')->unsigned();
$table->integer('school_id')->unsigned();
$table->integer('approved');
$table->string('application');
$table->timestamps();
$table->foreign('requisition_id')->references('id')->on('requisitions');
$table->foreign('school_id')->references('id')->on('schools');
});
}
RequisitionController.php
public function update(Request $request, SubmitApplication $submitApplication)
{
$submitApplication->approved = 1;
$submitApplication->save();
return redirect()->back();
}
我收到此错误。
SQLSTATE [HY000]:常规错误:1364字段'requisition_id'没有 有一个默认值(SQL:插入
submit_applications
(approved
,updated_at
,created_at
)值(1,2018-06-18 04:22:48,2018-06-18 04:22:48))
当我点击已批准的按钮时。它补充道。 id不想添加
也许我做错了?
答案 0 :(得分:1)
在 submit_applications 表格中,将nullable设置为 requisition_id
public function up()
{
Schema::create('submit_applications', function (Blueprint $table) {
$table->increments('id');
$table->integer('requisition_id')->nullable()->unsigned();
$table->integer('school_id')->nullable()->unsigned();
$table->integer('approved')->nullable(true);
$table->string('application')->nullable(true);
$table->timestamps();
$table->foreign('requisition_id')->references('id')->on('requisitions');
$table->foreign('school_id')->references('id')->on('schools');
});
}