0

On Laravel site using jenssegers/mongodb I have store method like :

public function store(ItemRequest $request)
{
    $request = request();
    try {
        $session = DB::getMongoClient()->startSession();
        $session->startTransaction();
        $insertData              = $request->all();
        $insertData['published'] = ! empty($insertData['published']);
        $item            = Item::create([
            'title'     => $insertData['title'],
            'text'      => $insertData['text'],
            'published' => $insertData['published'],
        ]);
        $session->commitTransaction();
    } catch (Exception $e) {
        $session->abortTransaction();
        return back()->withErrors(['message' => $e->getMessage()]);
    }


    return redirect(route('admin.items.edit', $item->_id))
        ->with('message', 'New item was successfully added')
        ->with('message_type', 'success');
}

But making test for this controller method I did not find how to get ID on new ite, generated on mongodb site :

public function testIsItemEditFormSubmittedWithSuccess()
{
    $this->withoutMiddleware();
    $item = \App\Models\Item::factory(Item::class)->make();

    // Test Action
    $response = $this->actingAs(self::$loggedAdmin, 'web')->post(route('admin.items.store'), $item->toArray());

    $newItemId = DB::getPdo()->lastInsertId();
    // If to uncomment line above I got "Error: Call to a member function lastInsertId() on null"
    $response->assertStatus(302);  // Redirection status
    $response->assertRedirect(route('admin.items.edit', [???])); // HOW can New ID on "store" method above ?
    $response->assertSessionHasNoErrors();
}

Any equvalent of lastInsertId for jenssegers/mongodb ?

"jenssegers/mongodb": "^3.9.2",
"laravel/framework": "^9.30.1",

Thanks in advance!

Petro Gromovo
  • 1,755
  • 5
  • 33
  • 91

1 Answers1

1

Check this issue. https://github.com/jenssegers/laravel-mongodb/issues/2451

Your laravel should be 9.31. So downgrade it to 9.30 and wait next release.

the_hasanov
  • 782
  • 7
  • 15
  • Now in my composer.json I have : "laravel/framework": "^9.30.1" and my phpstorm shows it as latest version. What have I to do ? Change it to "laravel/framework": "^9.30" and run composer update and which version have I to waite at https://github.com/laravel/framework ? – Petro Gromovo Sep 26 '22 at 12:02
  • composer require "laravel/framework":"9.30.1" – the_hasanov Sep 26 '22 at 12:06
  • Sorry, I did not understand my phpstorm shows "^9.30.1", which is higher current laravel/framework at github. When I have to wait for new release ? – Petro Gromovo Sep 26 '22 at 13:57
  • @PetroGromovo ^9.30.1 is meaning 9.3 >= version < 10.0 – the_hasanov Sep 26 '22 at 14:29
  • @PetroGromovo remove your vendor and composer.lock file and run composer install. Dont use composer update before new release – the_hasanov Sep 26 '22 at 14:34