Skip to content

Instantly share code, notes, and snippets.

[Illuminate\Database\QueryException]
SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or upda
te a parent row: a foreign key constraint fails (SQL: drop table if exists
`topics`)
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTopicsTable extends Migration
{
/**
* Run the migrations.
*
public function up()
{
Schema::create('topics', function (Blueprint $table) {
$table->increments('id');
$table->string('title')->nullable();
$table->string('duration')->nullable();
$table->timestamps();
$table->softDeletes();
ouput of request
"_token" => "AEVBOOFQToeqh0yhCiTdJR7peCerZeWpUAIKWwzj"
"category_id" => "1"
"topic_id" => "1"
"question_text" => "question"
"option1" => "ans1"
"option2" => "ans2"
"option3" => "ans3"
"option4" => "ans4"
public function store(Request $request)
{
$ques = new Question();
$ques->question_text = $request['question_text'];
$ques->code_snippet = $request['code_snippet'];
$ques->answer_explanation = $request['answer_explanation'];
$ques->more_info_link = $request['more_info_link'];
$ques->category_id = $request['category_id'];
$ques->topic_id = $request['topic_id'];
dd($ques->save());
2 question_idIndex int(10) UNSIGNED Yes NULL Change Change Drop Drop
Primary Primary
Unique Unique
Index Index
More
3 option varchar(255) utf8_unicode_ci Yes NULL Change Change Drop Drop
Primary Primary
Unique Unique
Index Index
More
public function store(Request $request)
{
$ques = new Question();
$ques->question_text = $request['question_text'];
$ques->code_snippet = $request['code_snippet'];
$ques->answer_explanation = $request['answer_explanation'];
$ques->more_info_link = $request['more_info_link'];
$ques->category_id = $request['category_id'];
$ques->topic_id = $request['topic_id'];
$ques->save();
$data = [];
$now = Carbon::now();
foreach($request->option as $key => $value) {
$data[] = [
'question_id' => $ques->id,
'answer' => $value,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
];
$ques = new Question();
$ques->question_text = $request->question_text;
$ques->code_snippet = $request->code_snippet;
$ques->answer_explanation = $request->answer_explanation;
$ques->more_info_link = $request->more_info_link;
$ques->category_id = $request->category_id;
$ques->topic_id = $request->topic_id;
$ques->save();
$data = [];
-- phpMyAdmin SQL Dump
-- version 4.5.4.1deb2ubuntu2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Nov 15, 2017 at 07:18 AM
-- Server version: 5.7.20-0ubuntu0.16.04.1
-- PHP Version: 7.0.22-0ubuntu0.16.04.1
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";