-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Are Transactions mandatory for obtaining the _id on Create? #2737
-
- Laravel-mongodb: v4.1.2
- Laravel: v 10.45.1
- php: 8.1
`use MongoDB\Laravel\Eloquent\Model;
class TestClass extends Model
{
protected $connection = 'mongodb';
protected $primaryKey = '_id';
protected $keyType = 'string';
}
`
Given the test model above, would it be necessary to have Transactions enabled to be able to obtain the ObjectId in the given way:
$test = new TestClass;
$test->save();
$test->refresh();
Doing this "$test->refresh()" always returns null, because the local instance ($test) cannot obtain the _id.
This may be common knowledge, but this has been driving me crazy, any help would be appreciated.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment
-
Hi @NfickettAIS, the transaction is not necessary to make it work. I've just tested your code with MongoDB server 7.0 and 4.4, and $test->_id
is correctly populated. Did you find the root cause of your issue?
Note that you don't need to specify $primaryKey
and $keyType
: _id
and string
are the default values set in the parent class.
Beta Was this translation helpful? Give feedback.