From 39e71e19656299194fa4d8372671e37d59c767a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Thu, 1 Dec 2022 17:35:21 +0100 Subject: [PATCH] Fix question crud - remover answer on create form - add brand_id onto fillable - readonly on brand (dont change brand after question create) - add cascade delete on question You should create questions the add answers --- app/Models/Question.php | 5 +-- ...173016_cascade_delete_question_answers.php | 36 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 database/migrations/2022_12_01_173016_cascade_delete_question_answers.php diff --git a/app/Models/Question.php b/app/Models/Question.php index 45797ea..855a86e 100644 --- a/app/Models/Question.php +++ b/app/Models/Question.php @@ -39,7 +39,8 @@ class Question extends Model */ protected $fillable = [ 'question', - 'type' + 'type', + 'brand_id' ]; /** @@ -77,6 +78,7 @@ class Question extends Model 'brand_id' => [ 'label' => Str::ucfirst(trans('models.classes.brand')), 'type' => CrudType::belongsTo(), + 'readonly' => true, 'belongsTo' => Brand::class, 'actions' => [ CrudAction::viewAny(), @@ -94,7 +96,6 @@ class Question extends Model 'actions' => [ CrudAction::viewAny(), CrudAction::view(), - CrudAction::create(), CrudAction::update(), ] ] diff --git a/database/migrations/2022_12_01_173016_cascade_delete_question_answers.php b/database/migrations/2022_12_01_173016_cascade_delete_question_answers.php new file mode 100644 index 0000000..37e5431 --- /dev/null +++ b/database/migrations/2022_12_01_173016_cascade_delete_question_answers.php @@ -0,0 +1,36 @@ +dropForeign(['question_id']); + $table->foreign('question_id')->references('id')->on('questions')->cascadeOnDelete(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // * Undo cascade delete (restrict as default) + Schema::table('answers', function (Blueprint $table) { + $table->dropForeign(['question_id']); + $table->foreign('question_id')->references('id')->on('questions'); + }); + } +}