#Eloquent with Mongodb gives empty _id object after create.

19 messages · Page 1 of 1 (latest)

devout moss
#

I followed multiple examples but without success.

Anyone a idea how to fix this?

dd($location->toJson());
returns:

"{"name":"1232137","address":{"street":"asdad","number":"24","zipcode":"1122AA","town":"adsad","country":"Nederland"},"loc":{"type":"Point","coordinates":[0,0]},"contactPers":null,"tel":null,"debnmr":"2023001","lastcheck":1721073386,"nextcheck":1721073386,"notes":null,"files":[],"updated_at":"2024-07-15T19:56:26.924000Z","created_at":"2024-07-15T19:56:26.924000Z","_id":{}}" // app/Http/Controllers/Locations/LocationsController.php:89
proven ocean
devout moss
#

Thank you for your response! When I do the refresh() i'll get the error: Call to undefined method MongoDB\Laravel\Query\Builder::compileWhereSub()

It looks like it is still in the query builder.

This is the code and i'm using mongodb/laravel-mongodb

$location = LocationModel::create([
            'name' => $request->input('name'),
            'address' => [
                'street' => $address['street'],
                'number' => $address['number'],
                'zipcode' => $address['zipcode'],
                'town' => $address['town'],
                'country' => $address['country']
            ],
            'loc' => $geo,
            'contactPers' => $request->input('contactpersoon'),
            'tel' => $request->input('tel'),
            'debnmr' => $request->input('debnmr'),
            'lastcheck' => strtotime($request->input('lastcheck')." UTC"),
            'nextcheck' => strtotime($request->input('nextcheck')." UTC"),
            'notes' => $request->input('notes'),
            'files' => $request->input('files')
        ]);

dd($location-toJson());
proven ocean
#

I don't have any projects running with Mongodb to troubleshoot this, but it seems that the object is stale for some reason. You probably have some other program to query MongoDB, like Compass, so you can check if the location created actually has an _id. If so, it is just a matter of making sure Laravel loads this properly. I have found these two similar issues, maybe they will help? https://github.com/mongodb/laravel-mongodb/issues/2679 and https://github.com/mongodb/laravel-mongodb/issues/791. They are old issues though.

GitHub

Laravel-framework Version: 10.31.1 PHP Version: 8.2.12 MongoDB extension version : 1.16.2 Laravel-mongodb Version: 4.0.0 Description: Steps to reproduce Route::get('test', function () { $us...

GitHub

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent) - Issues · mongodb/laravel-mongodb

#

Later today I can add mongodb to one of my projects to troubleshoot if you still didn't find a solution

devout moss
#

Thank you very much for your great effort. I will test the issues you posted above. When i look in the database i see an _id like I aspected but it just don't came back to me after the create. When I do $location->get(); I will get all the documents with the _id filledin.

proven ocean
#

I created a minimal project, created a simplified "Location" model, did a Location::create(['name' => 'something']) and the _id populated as expected. Could you share your LocationModel? I can try troubleshooting now that I have the project running.

devout moss
#

Yeah sure!

#
<?php
namespace App\Models\Locations;

use MongoDB\Laravel\Eloquent\Model;

class LocationModel extends Model {
    
    protected $collection = 'locations';
    
    protected $company = '';
    protected $address = '';
    protected $loc = '';
    protected $contactPers = '';
    protected $tel;
    protected $lastcheck;
    protected $nextcheck;
    protected $debnmr;
    protected $status;
    protected $notes;
    protected $files;
    protected $fillable = [
        'name',
        'address',
        'loc',
        'contactPers',
        'tel',
        'debnmr',
        'lastcheck',
        'nextcheck',
        'notes',
        'files'
    ];
}
proven ocean
#

I tested with this and it seemed to work as expected, weird. I'll have a quick break and then I'll try to troubleshoot it a bit more.

devout moss
#

Thank you sir

devout moss
#

Maybe its an bug in Mongodb/laravel 4.0

proven ocean
#

Maybe, let me check my mongodb version here. It seems that everything is working fine for me

proven ocean
#

"mongodb/laravel-mongodb": "^4.6" with a mongo:latest docker image

#

I would say there is nothing wrong with your code, so I'd look into upgrading laravel-mongodb, checking the mongo version, etc.

#

You can maybe try saving it differently, like:

$location = new LocationModel();
$location->name = 'name';
$location->save();

And see if that works better. But it should work with the code you have, I tried it and it worked fine.

devout moss
#

Yeah I tried the different way first but have the same result so i turned it the way like the example but have the same results.

proven ocean
#

I'm far from my pc right now, but have you tried upgrading the package? I tried replicating the issue but I couldn't. Maybe it is a mongodb setting?

devout moss
#

The update fixed my problem