Skip to content

Using the connection everywhere #58

@nbertal

Description

@nbertal

Hi again,
I would like to use the MongoDB connection outside of a Model (ie. a custom class not dependant of a specific collection).

So far:
DB::getMongoDB() does'nt work (dependency issue, couldn't resolve)
DB::connection('mongodb') returns NULL

What is the best practice?

A) still use your IoC ? Then how can I proceed?
B) Open the connection using the original Mongo PHP library, everytime I need it ? Isn't it efficient because it might trigger multiple connections instead of a single instance?

Thanks in advance!

Activity

jenssegers

jenssegers commented on Oct 23, 2013

@jenssegers
Contributor

What do you mean? Using this library outside of a Laravel project?

nbertal

nbertal commented on Oct 23, 2013

@nbertal
Author

Nope, for instance I want to use it in a class that is not a "Model" per say (but inside a L4 project where jenssegers/Laravel-MongoDB is present and configured).

jenssegers

jenssegers commented on Oct 23, 2013

@jenssegers
Contributor

You should be able to just use the query builder, like you would in Laravel: https://github.com/jenssegers/Laravel-MongoDB#query-builder

nbertal

nbertal commented on Oct 23, 2013

@nbertal
Author

That works indeed, but I need to get the MongoClient and MongoDB objects.

nbertal

nbertal commented on Oct 23, 2013

@nbertal
Author

At least the MongoDB. This I couldn't succeed outside of a Jenssegers\Mongodb\Model extended class..

jenssegers

jenssegers commented on Oct 23, 2013

@jenssegers
Contributor
nbertal

nbertal commented on Oct 23, 2013

@nbertal
Author

I suspect it doesn't work because it does not precise what connection to use, so it's using the default one (ie. in my case I use postgre):

call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Database\PostgresConnection' does not have a method 'getMongoDB'

So I thought I was a dependency issue, but I couldn't resolve it.

nbertal

nbertal commented on Oct 23, 2013

@nbertal
Author

Just to be clear and summarizing a little:

It seems impossible to access the MongoClient or MongoDB objects when not working with established Models if Mongo not the default DB connection.

nbertal

nbertal commented on Oct 23, 2013

@nbertal
Author

I developped my own connection class based on yours, now everything is fine.

Thank you!

waelwalid

waelwalid commented on Apr 19, 2019

@waelwalid

@nbertal

Hi , I have the same issue so could you please tell me what is your solution ?
Thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jenssegers@nbertal@waelwalid

        Issue actions

          Using the connection everywhere · Issue #58 · mongodb/laravel-mongodb