[英]Laravel Create Eloquent Instance from ManyToOne Relationship Error SQLSTATE[23000]
I want to create instance using Laravel 5 Eloquent Relationship. 我想使用Laravel 5 Eloquent Relationship创建实例。
I have 2 Migrations and 2 Eloquent Model. 我有2个迁移和2个雄辩的模型。
Companies Migration: 公司迁移:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCompaniesTable extends Migration {
public function up()
{
Schema::create('Companies', function(Blueprint $table)
{
$table->string('CompanyCode', 15);
$table->string('Name', 200);
$table->string('Type', 100);
$table->tinyInteger('IsActive')->default(1);
$table->timestamps();
$table->primary('CompanyCode');
});
}
public function down()
{
Schema::drop('Companies');
}
}
Company Model: 公司型号:
namespace App\Models\Setting\Organization;
use Illuminate\Database\Eloquent\Model;
class Company extends Model {
protected $fillable = ['CompanyCode', 'Name', 'Type'];
public function organizationUnits(){
return $this->hasMany('App\Models\Setting\Organization\OrganizationUnit', 'CompanyCode');
}
}
OrganizationUnits Migration: OrganizationUnits迁移:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateOrganizationUnitsTable extends Migration {
public function up()
{
Schema::create('OrganizationUnits', function(Blueprint $table)
{
$table->string('OrganizationUnitCode', 15); //PK
$table->string('CompanyCode', 15); //FK
$table->string('Name', 200);
$table->tinyInteger('IsActive')->default(1);
$table->timestamps();
$table->primary('OrganizationUnitCode');
$table->foreign('CompanyCode', 'OrgUnits_Company_FK')
->references('CompanyCode')
->on('Companies')
->onDelete('cascade');
});
}
public function down()
{
Schema::drop('OrganizationUnits');
}
}
OrganizationUnit Model: OrganizationUnit模型:
namespace App\Models\Setting\Organization;
use Illuminate\Database\Eloquent\Model;
class OrganizationUnit extends Model {
protected $table = "OrganizationUnits";
protected $fillable = ['OrganizationUnitCode', 'CompanyCode', 'Name'];
public function company(){
return $this->belongsTo('App\Models\Setting\Organization\Company', 'CompanyCode');
}
}
The relationship is one Company may have one or more OrganizationUnit, one OrganizationUnit must have one and only one Company. 关系是:一个公司可能拥有一个或多个OrganizationUnit,一个OrganizationUnit必须拥有一个且只有一个Company。
I tried to create new instance of OrganizationUnit in php artisan tinker
using this code: 我试图使用以下代码在
php artisan tinker
创建OrganizationUnit的新实例:
$company = \App\Models\Setting\Organization\Company::first();
$orgunit = $company->organizationUnits()->create(['OrganizationUnitCode' => 'abcdef']);
But Laravel gives the following error: 但是Laravel给出以下错误:
Illuminate\\Database\\QueryException with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'CompanyCode' cannot be null (SQL: insert into `OrganizationUnits` (`Org anizationUnitCode`, `CompanyCode`, `updated_at`, `created_at`) values (abcdef, , 2015-12-17 00:17:33, 2015-12-17 00:17:33))'
带有消息'SQLSTATE [23000]的Illuminate \\ Database \\ QueryException:违反完整性约束:1048列'CompanyCode'不能为空(SQL:插入到'OrganizationUnits`(“ Org anizationUnitCode`,`CompanyCode`,`updated_at`,`created_at` )值(abcdef,,2015-12-17 00:17:33,2015-12-17 00:17:33))'
Where did I go wrong? 我哪里做错了? Please help.
请帮忙。 I'm new to Laravel.
我是Laravel的新手。
It clearly says that CompanyCode cannot be null. 明确指出CompanyCode不能为null。 You can define it by hand or you can use the
increments
method on the Blueprint instance while creating your migration. 您可以手动定义它,也可以在创建迁移时在Blueprint实例上使用
increments
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.