【发布时间】:2021-04-28 20:10:37
【问题描述】:
由于某种未知原因,Model::create() 后返回的主键值不是数据库中的主键值。我尝试将“递增”设置为 false,因为我发现这对其他人有用,但对我没有任何作用。
Laravel 框架 8.34.0
型号
class Provider extends Model
{
protected $table = 'DoctorFacility';
public $primaryKey = 'DoctorFacilityId';
//public $incrementing = false; // tried this it just made the primary key empty
}
控制器
use App\Models\Provider;
class ProviderController extends Controller
{
public function create(Request $request)
{
new Provider;
$result = Provider::create([
'first_name' => 'Jane',
'last_name' => 'Doe'
]);
// returns 45004857 the first test, and 45004859 on the
// second test (so it is incrementing in some way?)
// while the actual id of the last created record
// is "62644" on the first test, and "62645" on
// the second test.
Log::critical($result->DoctorFacilityId);
}
}
【问题讨论】:
-
你能展示你的迁移吗?
-
我怀疑这是问题所在,但the Laravel docs show
$primaryKeyshould be protected,不公开。
标签: laravel-8