代码之家  ›  专栏  ›  技术社区  ›  Reno Wijoyo

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

  •  2
  • Reno Wijoyo  · 技术社区  · 7 年前

    这是我的模型

    <?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'
    ];
    }
    

    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();
        });
    

    在我的控制器中:

    $org = OrganizationProfile::where('organization_id', $id)->first();
    
        if ($org == null) {
            $org = new OrganizationProfile();
            $org->organization_id = $id;
        } 
        $org->fill($request->input())->save();
    

    这是错误: enter image description here

    eloquent尝试使用“id”作为参考来更新更改的值。我没有“id”字段,我有一个“organization\u id”作为主键和外键。如果我不更改$filleble中列出的任何值,则不会显示此错误。

    提前感谢

    3 回复  |  直到 7 年前
        1
  •  5
  •   Björn    7 年前

    Eloquent将假定每个表都有一个主键列,名为 id 。您可以使用 $primaryKey 像这样:

    <?php
    namespace App;
    use Illuminate\Database\Eloquent\Model;
    
    class OrganizationProfile extends Model
    {
        protected $primaryKey = 'organization_id';
    }
    
        2
  •  0
  •   Fauzi PadLaw    7 年前

    将模型更改为:

      <?php
        namespace App;
        use Illuminate\Database\Eloquent\Model;
    
        class OrganizationProfile extends Model
        {
            protected $primaryKey = 'organization_id';
    
            protected $fillable = [
            'logo', 
            'number_of_employees', 
            'siup', 'npwp', 'year_established', 'join_ppsdm', 'industry_sector',
            'address', 'city', 'province', 'country', 
            'postal_code',
            'organization_phone', 
            'organization_email'
        ];
        }
    

    如果要禁用递增。只需在模型中添加下面的代码

    public $incrementing = false;
    
        3
  •  -1
  •   Volkan Yılmaz    7 年前

    你可以试试这个;

    $org->fill($request->except('id'))->save();