Php Eloquent不通过关联插入非整数

Php Eloquent不通过关联插入非整数,php,laravel,eloquent,Php,Laravel,Eloquent,我为lang创建了以下迁移: class CreateLangTable extends Migration { public function up() { Schema::create('lang', function (Blueprint $table) { $table->string('code')->primary(); $table->string('name'); $table->timestamp

我为lang创建了以下迁移:

class CreateLangTable extends Migration
{

public function up()
{
    Schema::create('lang', function (Blueprint $table) {
        $table->string('code')->primary();
        $table->string('name');
        $table->timestamps();
    });
}


}
class CreateMachineryCategoriesTranslationTable extends Migration
{

public function up()
{
    Schema::create('machinery_categories_translation', function (Blueprint $table) {
        $table->increments('id');
        $table->string('lang_code')->index();
        $table->foreign('lang_code')->references('code')->on('lang')->onDelete('cascade')->onUpdate('cascade');
        $table->integer('machinery_categories_id')->unsigned()->index();
        $table->foreign('machinery_categories_id')->references('id')->on('machinery_categories')->onDelete('cascade')->onUpdate('cascade');
        $table->string('name')->nullable();
        $table->timestamps();
    });
}
但当我尝试播种时:

class MachineryCategoriesTranslationTableSeeder extends Seeder
{

public function run()
{
    $el = Lang::find('el');
    $en = Lang::find('en');

    $loaderCat = new MachineryCategory();
    $loaderCat->save();

    $loaderCatEl = new MachineryCategoriesTranslation();
    $loaderCatEl->machineryCategories()->associate($loaderCat);
    $loaderCatEl->lang()->associate($el);
    $loaderCatEl->name = 'Φορτωτές';
    $loaderCatEl->save();

    $loaderCatEn = new MachineryCategoriesTranslation();
    $loaderCatEn->machineryCategories()->associate($loaderCat);
    $loaderCatEn->lang()->associate($en);
    $loaderCatEn->name = 'Loaders';
    $loaderCatEn->save();
}
}

我得到了错误

 SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or    update a child row: a foreign key constraint fails (`ps4cat`.`machinery_categories_translation`, CONSTRAINT `machinery_categories_translation_lang_code_foreign` FOREIGN KEY (`lang_code`) REFERENCES `lang` (`code`) ON DELETE CASCADE ON UPDATE CA) 

(SQL: insert into `machinery_categories_translation` (`machinery_categories_id`, `lang_code`, `name` , `updated_at`, `created_at`) values (1, 0, Φορτωτές, 2016-04-08 16:24:06, 2016-04-08 16:24:06))  

其中
lang\u code
value
中的
0
明显错误。有什么问题吗?

你能把
Lang
MachineryCategoriesTranslation
模型附加上去吗?我不确定我是否遵循了将模型中的代码添加到你的帖子中的步骤。