【发布时间】:2017-11-07 14:12:34
【问题描述】:
我在删除项目中的数据时遇到问题.. 我有两个没有任何关系的表数据库
表单的表格
Schema::create('forms', function (Blueprint $table) {
$table->increments('id');
$table->string('FormName', 200);
$table->string('GroupID', 100)->nullable();
$table->integer('EditorType');
$table->integer('LanguageID')->nullable()->default(1);
$table->integer('PageTemplateID')->nullable();
$table->integer('GadgetID')->nullable();
$table->string('Categories', 200)->nullable();
$table->timestamps();
});
内容设置表
Schema::create('content_settings', function (Blueprint $table) {
$table->increments('id');
$table->integer('language_id');
$table->integer('gadget_id');
$table->string('ContentSettingName', 100);
$table->text('ContentSettingValue');
$table->timestamps();
});
控制器
public function destroy($id)
{
$form = Form::findOrFail($id);
$pages = Form::where('GroupID', $form->GroupID)->get()->pluck('id')->toArray();
$homepage = ContentSetting::where('language_id', $form->LanguageID)
->where('gadget_id', $form->GadgetID)
->where('ContentSettingValue', $form->id)
->pluck('ContentSettingValue')->first();
if(!in_array($homepage, $pages)){
$deletePages = Form::where('GroupID', $form->GroupID)->delete();
}
return response()->json($homepage);
}
当我将数据存储到数据库时,我将直接存储 2 行数据。因为在我的表格中,我有列语言ID,并且2行数据具有相同的GroupID..问题是我想删除数据时。
forms.id 与content_settings.ContentSettingValue 相连。因此,当我存储数据后,我可以选择其中一个数据为default homepage。如果我对默认主页的数据单击删除按钮,则会出现警报('您不能删除默认主页'),但是如果我对不是默认主页的数据单击删除按钮,但它具有相同GroupID 与默认主页。它将删除两行数据(换句话说,默认主页也将被删除)。如果我检查其中一个数据是默认主页而另一个不是默认主页,但它们具有相同的 GroupID,我该如何处理它,我无法删除它们。
【问题讨论】: