Install Laravel-MongoDB
My current version of laravel is 7.8, so install 3.7.
See the official documentation for details: https://github.com/jenssegers/laravel-mongodb
Laravel version Compatibility
Laravel | Package |
---|---|
4.2.x | 2.0.x |
5.0.x | 2.1.x |
5.1.x | 2.2.x or 3.0.x |
5.2.x | 2.3.x or 3.0.x |
5.3.x | 3.1.x or 3.2.x |
5.4.x | 3.2.x |
5.5.x | 3.3.x |
5.6.x | 3.4.x |
5.7.x | 3.4.x |
5.8.x | 3.5.x |
6.x | 3.6.x |
7.x | 3.7.x |
8.x | 3.8.x |
- Recommended components
|
composer require jenssegers/mongodb ^3.6 -vvv |
If nothing else, you can see:
So even if the installation is successful!
- Registration Service
- In the app/config/app.php file
|
|
- Add Facades
|
|
code show as below:
'providers' => [
....
/**
* Mongodb
*/
Jenssegers\Mongodb\MongodbServiceProvider::class,
],
'aliases' => [
....
'Mongo' => Jenssegers\Mongodb\MongodbServiceProvider::class,
]
- Modify the database configuration file config/database.php
'default' => env('DB_CONNECTION', 'mysql'),
改成:
'default' => env('DB_CONNECTION', 'mongodb'),
If the default is not mongo, this operation will not be used for so long.
Use it in the controller: Okay, let's use it next:
$attchment = [
'id' => '1',
'appointment_id' => '5DC3B968-51B6-44C5-8DAC-65E7F7641F80.xlsx',
'attchment_type' => 'dd',
'original_file_name' => '录入表 .xlsx',
'file_name' => 'C59713E0-C4BC-4469-81DB-7575C485E552.xlsx',
'file_path' => '20201030/C59713E0-C4BC-4469-81DB-7575C485E552.xlsx',
'upload_id' => 1,
'upload_group_id' => 1,
'fileData' => ['order_id'=>202010310819508286],
]
DB::connection('mongodb') //选择使用mongodb
->collection('file') //Choose to use file collection-
>insert($attchment);
Write the following processing in mode:
Then execute it, you can see the data of mongo:
If I want to query, then execute:
$fileList = DB::connection('mongodb')->collection('file')->where('filekey',$uid)->first();