Laravel - Laracasts

时间:2015-10-05 09:49:57

标签: laravel laravel-5.1

我是Laravel的新手,我努力想知道问题出在哪里,但似乎无法找到它。在此先感谢大家..

这是错误:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`homestead`.`articles_tag`, CONSTRAINT `articles_tag_tag_id_foreign` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`) ON DELETE CASCADE) (SQL: insert into `articles_tag` (`articles_id`, `created_at`, `tag_id`, `updated_at`) values (4, 2015-10-05 09:33:18, 0, 2015-10-05 09:33:18))

这是Tag eloquent模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Tag extends Model
{
    protected $fillable = array();

    //get the articles associated with the given tag
    public function articles(){
        return $this->belongsToMany('App/Article');
    } 
}

这是文章雄辩的模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class Articles extends Model
{
    protected $fillable = [
        'title',
        'body',
        'published_at',
        'user_id' 
    ];

    protected $dates = ['published_at'];

    public function scopePublished($query){
        $query->where('published_at', '<=', Carbon::now());
    }

    public function scopeUnpublished($query){
        $query->where('published_at', '>', Carbon::now());
    }

    public function setPublishedAtAttribute($date){
        $this->attributes['published_at'] = Carbon::parse($date);
    }

    //An article is owned by a user
    public function user(){
        return $this->belongsTo('App\User');
    }

    //An article has many tags
    //get the tags associated with the tables
    public function tags(){
        return $this->belongsToMany('App\Tag')->withTimestamps(); 
    }

    /*
    public function setPasswordAttribute($password){
        $this->attributes['password'] = mcrypt($password);
    } //key is to user attributes
    */
}

这是我的创建迁移:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTagsTable extends Migration
{

    public function up()
    {
        Schema::create('tags', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });

        //we are trying to connect articles and tags
        //really it should we article_tag not articles_tag ;)
        Schema::create('articles_tag', function (Blueprint $table) {
            $table->integer('articles_id')->unsigned()->index();
            $table->foreign('articles_id')->references('id')->on('articles')->onDelete('cascade');

            $table->integer('tag_id')->unsigned()->index();
            $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');

            $table->timestamps();
        });
    }


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

这是文章迁移:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class Articles extends Migration
{

    public function up()
    {
        Schema::create('articles', function(Blueprint $table){
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->string('title');
            $table->text('body');
            $table->timestamps();
            $table->timestamp('published_at');

            $table->foreign('user_id')
                  ->references('id')
                  ->on('users')
                  ->onDelete('cascade');
        });
    }

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

3 个答案:

答案 0 :(得分:0)

在迁移表articles_tag时,尝试将该表的代码修改为(并确保首先有文章表)

Schema::create('articles_tag', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('articles_id')->unsigned();
    $table->foreign('articles_id')->references('id')->on('articles')->onDelete('cascade');

    $table->integer('tag_id')->unsigned();
    $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');

    $table->timestamps();
});

答案 1 :(得分:0)

Try to separate creating and setting constraints like:

Schema::create('articles_tag', function (Blueprint $table) {
        $table->integer('articles_id')->unsigned()->index();
        $table->integer('tag_id')->unsigned()->index();
        $table->timestamps();            
});

Schema::table('articles_tag', function (Blueprint $table) {
        $table->foreign('articles_id')->references('id')->on('articles')->onDelete('cascade');   
        $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');

});

答案 2 :(得分:0)

您的迁移文件中应该 article_tag (单词单数)而不是 articles_tag ,如下所示:

Schema::create('article_tag', function (Blueprint $table) {

如果您仍想用作 articles_tag ,则应明确指定数据透视表。您应该分别在标记文章模型中执行以下操作:

Tag.php

public function articles(){
    return $this->belongsToMany('App/Article', 'articles_tag');
} 

Article.php

public function tags(){
    return $this->belongsToMany('App\Tag', 'articles_tag')->withTimestamps(); 
}

有关详细信息,请访问Laravel Docs for Eloquent Many-to-Many Relationships