How to get mongodb query log in laravel?

by scotty_walker , in category: PHP Frameworks , 5 months ago

How to get mongodb query log in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by domenico , 5 months ago

@scotty_walker 

To get the MongoDB query log in Laravel, you can use the following steps:

  1. Install the MongoDB package for Laravel by running the following command:
1
composer require jenssegers/mongodb


  1. Publish the configuration file by running the following command:
1
php artisan vendor:publish --provider="JenssegersMongodbMongodbServiceProvider"


  1. Open the config/database.php file and set the default database connection to MongoDB. You can do this by setting the default value to mongodb in the connections array:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
'connections' => [
    'mongodb' => [
        'driver'   => 'mongodb',
        'host'     => env('DB_HOST', 'localhost'),
        'port'     => env('DB_PORT', 27017),
        'database' => env('DB_DATABASE', 'database'),
        'username' => env('DB_USERNAME', ''),
        'password' => env('DB_PASSWORD', ''),
        'options'  => []
    ]
],


  1. Enable logging in the MongoDB connection by setting the log option to true in the config/database.php file:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
'connections' => [
    'mongodb' => [
        'driver'   => 'mongodb',
        'host'     => env('DB_HOST', 'localhost'),
        'port'     => env('DB_PORT', 27017),
        'database' => env('DB_DATABASE', 'database'),
        'username' => env('DB_USERNAME', ''),
        'password' => env('DB_PASSWORD', ''),
        'options'  => [
            'log' => true
        ]
    ]
],


  1. You can now run your queries as usual in your Laravel application, and the MongoDB queries will be logged in the Laravel log files.


By following these steps, you can easily get the MongoDB query log in Laravel using the jenssegers/mongodb package.