laravel 5.6 fill()尝试使用不存在的'id'字段更新表

Ren*_*oyo 2 laravel eloquent

这是我的模特

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class OrganizationProfile extends Model
{
    protected $fillable = [
    'logo', 
    'number_of_employees', 
    'siup', 'npwp', 'year_established', 'join_ppsdm', 'industry_sector',
    'address', 'city', 'province', 'country', 
    'postal_code',
    'organization_phone', 
    'organization_email'
];
}
Run Code Online (Sandbox Code Playgroud)

OrganizationProfile的迁移表

Schema::create('organization_profiles', function (Blueprint $table) {
        $table->unsignedInteger('organization_id');
        $table->primary('organization_id');
        $table->foreign('organization_id')->references('id')->on('organizations')->onDelete('cascade');
        $table->string('logo')->nullable();
        $table->integer('number_of_employees')->nullable();
        $table->string('siup')->nullable();
        $table->string('npwp')->nullable();
        $table->integer('year_established')->nullable();
        $table->integer('join_ppsdm')->nullable();
        $table->string('industry_sector')->nullable();
        $table->text('address')->nullable();
        $table->string('city')->nullable();
        $table->string('province')->nullable();
        $table->string('country')->nullable();
        $table->string('postal_code')->nullable();
        $table->string('organization_phone')->nullable();
        $table->string('organization_email')->nullable();
        $table->text('meta')->nullable();
        $table->timestamps();
    });
Run Code Online (Sandbox Code Playgroud)

这在我的控制器中:

$org = OrganizationProfile::where('organization_id', $id)->first();

    if ($org == null) {
        $org = new OrganizationProfile();
        $org->organization_id = $id;
    } 
    $org->fill($request->input())->save();
Run Code Online (Sandbox Code Playgroud)

这是错误: 在此输入图像描述

eloquent尝试使用'id'作为参考来更新更改的值.我没有'id'字段,我有'organization_id'作为主键和外键.如果我不更改$ fillable中列出的任何值,则不会显示此错误.

提前致谢

Bjö*_*örn 5

Eloquent将假设每个表都有一个名为的主键列id.您可以$primaryKey像这样覆盖它:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class OrganizationProfile extends Model
{
    protected $primaryKey = 'organization_id';
}
Run Code Online (Sandbox Code Playgroud)