Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Are Transactions mandatory for obtaining the _id on Create? #2737

Unanswered
NfickettAIS asked this question in Q&A
Discussion options

  • 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.

You must be logged in to vote

Replies: 1 comment

Comment options

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.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /