这是我的模特
<?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();
eloquent尝试使用'id'作为参考来更新更改的值。我没有'id'字段,我有'organization_id'作为主键和外键。如果我不更改$ fillable中列出的任何值,则不会显示此错误。
提前致谢
答案 0 :(得分:5)
Eloquent将假设每个表都有一个名为id
的主键列。您可以使用$primaryKey
覆盖它,如下所示:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class OrganizationProfile extends Model
{
protected $primaryKey = 'organization_id';
}
答案 1 :(得分:0)
将模型更改为:
<?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;
答案 2 :(得分:-1)
你可以试试这个;
$org->fill($request->except('id'))->save();