Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use MongoDB Database in Laravel Framework

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to use the MongoDB database in the Laravel framework", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use the MongoDB database in the Laravel framework" this article.

1. First determine which version of the Laravel framework you use, and then decide which MongoDB for composer. I use Laravel 8, so I composer 3.8 MongoDb.

2. Execute the composer command and download it. I am using the second command

Composer require jenssegers/mongodb ^ 3.8-vvvcomposer require jenssegers/mongodb:3.8-- ignore-platform-reqs

3. An error may be reported at this time, so do the following

Php installs mongodb extension first (mongodb needs to be installed first on connected servers or locally)

Download address: https://pecl.php.net/package/mongodb

Choose here according to your PHP version, mine is 7.3.4, so I choose 1.6.0

Click in and pull to the bottom.

4. Copy the php_mongodb.dll file in the downloaded package to the ext folder under php7.3.4

5. Write extension=mongodb in php.ini

6. Cmd executes php-v to check whether it is running properly.

7. Use the composer command to download again. If nothing happens, you can see:

8. This means that the installation is successful

9. Register the service in the app/config/app.php file

Jenssegers\ Mongodb\ MongodbServiceProvider::class

'Mongo' = > Jenssegers\ Mongodb\ MongodbServiceProvider::class

10. Modify the database configuration file config/database.php

'mongodb' = > [' driver' = > 'mongodb',' host' = > env ('MONGODB_HOST',' 127.0.0.1'), 'port' = > env (' MONGODB_POST', '27017'),' database' = > env ('MONGODB_DB',' phone'), 'username' = > env (' MONGODB_USER','') 'password' = > env (' MONGODB_PASSWORD','),]

11. Add to the .env file

MONGODB_HOST=127.0.0.1MONGODB_POST=27017MONGODB_DB=phoneDB_USERNAME=DB_PASSWORD=

12. Then you can use it in the controller.

/ / add pubulic function index () {DB::connection ('mongodb') / / choose to use mongodb-> collection (' users') / / choose to use users collection-> insert ([/ / insert data 'name' = >' tom', 'age' = > 18]) } $res = DB::connection ('mongodb')-> collection (' phone')-> get ()-> toArray (); / / query all data dd ($res)

13. Query conditions can also be added when querying.

$res = DB::connection ('mongodb')-> collection (' phone')-> where ('name','tom')-> first ()

14. Modify a certain data

DB::connection ('mongodb')-> collection (' phone')-> where ('_ id', '5cf71b34e14620598643d23b')-> update ([' name'= > '123'])

15. Delete some data

$res = DB::connection ('mongodb')-> collection (' phone')-> where ('name','tom')-> delete ()

16. We can also use the model layer to create a new model layer

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report