Laravel外键完整性约束违规

时间:2016-04-22 11:48:57

标签: php database laravel laravel-5 laravel-5.1

我在Laravel 5.1中有两个表,我使用以下迁移创建了这些表:

class CreateProductsTable extends Migration
{

    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('email');
            $table->integer('number_of_chapters');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('products');
    }
}

和第二个:

class CreateChaptersTable extends Migration
{

    public function up()
    {
        Schema::create('chapters', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('chapter_id')->unsigned();
            $table->time('input-chapter-start');
            $table->time('input-chapter-end');
            $table->timestamps();
        });

        Schema::table('chapters', function($table) {
            $table->foreign('chapter_id')->references('id')->on('products');
        });
    }

    public function down()
    {
        Schema::drop('chapters');
    }
}

我的产品型号如下所示:

<?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;

    class Product extends Model
    {

        protected $table = 'products';

        protected $fillable = ['email', 'title', input-chapter-start', 'input-chapter-end'];

        public static $rules = [
            'email'                => 'required|email|max:50',
            'title'                => 'required|max:50'
        ];

        public function Chapters()
        {
            return $this->belongsTo('App\Chapters');
        }

        $Chapters = new Chapters;

    }

我的章节模型看起来像这样:

<?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;

    class Chapters extends Model
    {

        protected $table = 'chapters';

        protected $fillable = ['input-chapter-start', 'input-chapter-end'];

        public function product()
        {
            return $this->belongsTo('App\Product');
        }
    }

现在的问题是我的数据库中的章节表中没有任何值。我该如何管理?

如果我尝试在控制器中保存数据,我会收到以下错误消息:

  

Connection.php第651行中的QueryException:SQLSTATE [23000]:完整性   约束违规:1452无法添加或更新子行:异类   键约束失败(html_genchapters,CONSTRAINT   chapters_chapter_id_foreign FOREIGN KEY(chapter_id)参考   productsid))(SQL:插入chaptersupdated_at,   created_at)值(2016-04-22 11:32:04,2016-04-22 11:32:04))

我尝试保存产品的控制器如下所示:

public function createProduct(Request $request)
    {
        $productRom        = new ProductRom;

        $romChapters       = new RomChapters($request->all());
        $dataAll           = $request->all();
        $projectEmail      = $request->input('email');
        $projectTitle      = $request->input('title');
        $projectChGetStart = $request->input('input-chapter-start');
        $projectChGetEnd   = $request->input('input-chapter-end');

        Auth::user()->rom_chapters()->save($romChapters);
        $romChapters->save();

        $data = $request->only('email', 'title', 'filename', 'number_of_chapters');

        $inputForXml       = array($projectTitle);
        $projectChTimes    = array($projectChGetStart, $projectChGetEnd);

        # Validate Rom Input
        $validator = Validator::make($request->all(), ProductRom::$rules);

        # if validation fails return Erros
        if($validator->fails())
        {
            return Redirect::back()->withInput()->withErrors($validator);
        }

        $productRom->fill($data);

        if($productRom->save())
        {
        ...
        }
    }

0 个答案:

没有答案