Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bart's avatar
Level 13

Laravel 5.1 and MongoDB

Hey @JeffreyWay,

in my opinion it would be amazing if you could make a series about Laravel (5.1) and MongoDB. I played around with these two guys during the last few days but I don't really get a simplicity of Laravel and Eloquent like I would expect it.

In my opinion there are two packages which will help connecting a Laravel project with a MongoDB.

Thank you so much and I think this will help a lot of other Laravel guys here in the community. So what do you guys think about such a series?

0 likes
8 replies
iceheat's avatar

Hi Bart,

I'm in the same boat as you. I'm using Jenssenger's package. The way I found around it is to search through the parent class using the whereRaw method

class Car extends Eloquent
{

public static function findByID($id)
{
$showroom = Showroom::whereRaw(array('car._id' => new MongoId($id)))->first();
$car = $showroom->car->find($id);
return $car;
}

}

Whenever I need to search for a document that is embedded, I just used the findById() instead of find().

I hope this helps

1 like
bart's avatar
Level 13

Yeah @iceheat I played around with this stuff and in my opinion you are also able to do things like this:

// Search in post comments ...
$posts = App\Post::where('comments.content', 'LIKE', '%temporibus%')->get();

// ... when you extend your post model like this
public function comments()
{
    return $this->embedsMany('App\Comment');
}

I'm using jensseger's package as well.

rano_santoso's avatar

hello sir. im also on developing laravel 5.1 with mongodb.

i have a bit problem in auth and registring function. it said "ErrorException in Builder.php line 203". i know maybe its related with the query that default authcontroller create. should i changed it completely? or there is a way to change the querybuilder?

thank you :D

Gate6's avatar

Hello all, i've been unable to install the 'composer require jenssegers/mongodb' properly on windows. Added mongodb extension which is now showing in extensions list (not in phpinfo print). Still when i try to install jenssegers, i get following errors:

Your requirements could not be resolved to an installable set of packages. Problem 1

  • jenssegers/mongodb v3.1.0 requires mongodb/mongodb ^1.0.0 -> satisfiable by mongodb/mongodb[1. 0.0, 1.0.1, 1.0.2, 1.0.3].

    • jenssegers/mongodb v3.1.1 requires mongodb/mongodb ^1.0.0 -> satisfiable by mongodb/mongodb[1. 0.0, 1.0.1, 1.0.2, 1.0.3].
    • mongodb/mongodb 1.0.3 requires ext-mongodb ^1.1.0 -> the requested PHP extension mongodb is mi ssing from your system.
    • mongodb/mongodb 1.0.2 requires ext-mongodb ^1.1.0 -> the requested PHP extension mongodb is mi ssing from your system.
    • mongodb/mongodb 1.0.1 requires ext-mongodb ^1.1.0 -> the requested PHP extension mongodb is mi ssing from your system.
    • mongodb/mongodb 1.0.0 requires ext-mongodb ^1.1.0 -> the requested PHP extension mongodb is mi ssing from your system.
    • Installation request for jenssegers/mongodb ^3.1 -> satisfiable by jenssegers/mongodb[v3.1.0, v3.1.1].

    To enable extensions, verify that they are enabled in those .ini files:

    • C:\wamp\bin\php\php5.5.12\php.ini You can also run php --ini inside terminal to see which files are used by PHP in CLI mode.

Installation failed, reverting ./composer.json to its original content.

sharkreader's avatar

I've got jenssegers/mongodb 3.1 installed with Laravel 5.3. My environment variables are setup, and my database is configured. I can use MongoDB outside of Laravel and all is fine, but when I enter something into a form in Laravel, the data is not persisted in the database. I don't know where the data is going. I've been stuck here for three days! Unfortunately, I've not found one comprehensive video on the internet for configuring MongoDB with Laravel demonstrating success.

Can anyone point me to a good Laravel/MongoDB tutorial?

orrd's avatar

@sharkreader if you're already talking about entering data into a form, you're probably getting too far ahead of yourself. I would start by testing to see if you can just create and save a MongoDB Eloquent record. Read the Laravel-MongoDB readme and start with creating a test model class, then see if you can save it (see the "Saving a new model" section).

semicolon's avatar

@iceheat I was having a problem getting inner documents in a collection , your answer inspired me and worked for me thanks a lot $userInvoices = user_invoice::whereRaw(array('user.$id' => new ObjectID($loggedInUser->_id)))->get();

Please or to participate in this conversation.