我的CRUD应用程序出现奇怪的错误。 onUpdate->('cascade')
有效,但不适用于onDelete->('set null')
。如果我在数据库中手动删除它,它会起作用,但不适用于控制器中的删除操作。请在下面查看我的迁移文件。
class CreatePositionsTable extends Migration
{
public function up()
{
Schema::create('positions', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('department_id')->unsigned()->nullable();
$table->foreign('department_id')->references('id')->on('departments')
->onDelete('set null')->onUpdate('cascade');
$table->bigInteger('section_id')->unsigned()->nullable();
$table->foreign('section_id')->references('id')->on('sections')
->onDelete('set null')->onUpdate('cascade');
$table->string('position_name', 100);
$table->string('n_level', 10)->nullable();
$table->string('job_description')->nullable();
$table->softDeletes();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('positions');
}
}
我的模型(如果有影响)
Positions.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Iatstuti\Database\Support\CascadeSoftDeletes; //composer require iatstuti/laravel-cascade-soft-deletes
use App\SalaryMatrix;
use App\Employee;
class Position extends Model
{
protected $fillable = ['department_id', 'section_id', 'position_name', 'n_level', 'job_description'];
use SoftDeletes, CascadeSoftDeletes;
protected $softCascade = ['deleted_at'];
protected $cascadeDeletes = ['salaryMatrix', 'employee'];
public function department () {
return $this->belongsTo('App\Department');
}
public function section () {
return $this->belongsTo('App\Section');
}
public function salaryMatrix() {
return $this->hasOne('App\SalaryMatrix', 'position_id');
}
public function employee() {
return $this->hasMany('App\Employee', 'position_id');
}
}
PositionController.php
public function destroy($id) {
$position = Position::findOrFail($id);
$position->delete();
}
注意:此项目仅部署在localhost中。