我试图使用laravel 5.7删除3关系表上的数据,当我在2关系表上尝试它时,它运行良好,但是当我在3关系表中尝试时,它不起作用。>
这是我的桌子,像这样:
表1:group_access
|----|------------| | id | group_name | |----|------------| | 67 | test | |----|------------|
表2:fa_group_access
|----|-----------------|--------| | id | group_access_id | name | |----|-----------------|--------| | 1 | 67 | john | |----|-----------------|--------|
表3:survey_group_access
|----|-----------------|---------------| | id | group_access_id | code_survey | |----|-----------------|---------------| | 1 | 67 | SF-001 | |----|-----------------|---------------|
这是我在控制器中删除数据的功能:
public function destroy($id)
{
$group = Groups::findOrFail($id);
if($group->delete())
{
Survey_group_access::where('group_access_id', $id)->get();
FA_Group_Access::where('group_access_id', $id)->get();
return response()->json(['status'=>'success']);
}
}
我遇到了这样的错误sql:
SQLSTATE [23000]:[Microsoft] [SQL Server的ODBC驱动程序11] [SQL 服务器] DELETE语句与REFERENCE约束冲突 “ survey_group_access_group_access_id_foreign”。发生冲突 在数据库“ aetra2”的表“ dbo.survey_group_access”的列中 “ group_access_id”。 (SQL:从[group_access]中删除,其中[id] = 67)
如何解决此错误?
答案 0 :(得分:2)
在group_access模型中定义关系
public function fa_group_access()
{
return $this->hasMany('fa_group_access');
}
public function survey_group_access()
{
return $this->hasMany('survey_group_access');
}
删除记录和相关记录:
$group_access = Group_access::find($id);
// delete related
$group_access->fa_group_access()->delete();
$group_access->survey_group_access()->delete();
$group_access->delete();
我不测试代码,可能是语法错误,但应该是 像这样的东西。
我希望这会有所帮助
答案 1 :(得分:1)
这对于Model Events来说是一个很好的用例。我的操作方式如下:
<?php
namespace App\Models\Administrator;
use Illuminate\Database\Eloquent\Model;
use App\Models\Administrator\Users;
class Groups extends Model
{
protected $table = 'group_access';
public static function boot() {
parent::boot();
static::deleting(function($groups) {
$groups->survey_group_access()->delete();
$survey_group_access->users()->delete();
});
}
public function fa_group_access()
{
return $this->hasMany(FA_Group_Access::class,'group_access_id','id');
}
public function survey_group_access()
{
return $this->hasMany(Survey_group_access::class,'group_access_id','id');
}
}
..和您的控制器方法将是:
public function destroy($id)
{
$group = Groups::findOrFail($id);
if($group->delete()) {
return response()->json(['status'=>'success']);
}
}
答案 2 :(得分:1)
您可以在删除时使用层叠进行此操作。您可以从这里https://laravel.com/docs/5.7/migrations#foreign-key-constraints获取参考。
您可以更新迁移:(尽管我的类名只是假设,您可以理解,我也假设您是从迁移而不是从phpMyAdmin创建数据库的)
public function up()
{
Schema::create('fa_gropu_access', function (Blueprint $table) {
$table->increments('id');
$table->integer('group_access_id')->unsigned()->index();
$table->string('name');
$table->foreign('group_access_id ')->references('id')->on('group_access')->onDelete('cascade');
});
}
public function up()
{
Schema::create('survey_access_group', function (Blueprint $table) {
$table->increments('id');
$table->integer('group_access_id ')->unsigned()->index();
$table->string('code_survey');
$table->foreign('group_access_id ')->references('id')->on('group_access')->onDelete('cascade');
});
}
完成此操作后,每当您尝试使用$group_access->delete();
进行删除时,这两个相关数据将被自动删除。