I have been having this error on Laravel 11 when I try to upload an Excel document where my database is a MongoDB instance:
Transaction numbers are only allowed on a replica set member or mongos
Steps to reproduce
- I installed
Laravel 11
composer create-project laravel/laravel
- I added
Laravel Mongodbcomposer package
composer require mongodb/laravel-mongodb
- I added the
Laravel Excelcomposer package
composer require maatwebsite/excel:^3.1
Added some User Interface to facilitate the upload of the document. I am trying to upload assets into the web platform.
Created an AssetsImport document
php artisan make:import AssetsImport
The part that's producing the error is on this file,
class AssetsImport implements ToCollection
{
public function collection(Collection $collection)
{
foreach ($collection as $key => $row) {
$asset = Asset::where('asset_no', $row[2])->first();
if($asset) {
continue;
}
// add the upload code
....
}
}
}
The above script is meant to check if there's an existing Asset. If it's there, then don't add it (Continue with the foreach loop).
After checking this similar question, I still could not find a solution for this. Is this the correct way of implementing, and if so, how do I go about solving this error. I don't have control over the database (there's a Server Admin) so I cannot just add a replica set. Kindly assist.
This is the stacktrace of the error
{
"message": "Transaction numbers are only allowed on a replica set member or mongos",
"exception": "MongoDB\\Driver\\Exception\\BulkWriteException",
"file": "/home/anto/smartify/saptest/vendor/mongodb/mongodb/src/Operation/InsertOne.php",
"line": 126,
"trace": [
{
"file": "/home/anto/smartify/saptest/vendor/mongodb/mongodb/src/Operation/InsertOne.php",
"line": 126,
"function": "executeBulkWrite",
"class": "MongoDB\\Driver\\Server",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/mongodb/mongodb/src/Collection.php",
"line": 878,
"function": "execute",
"class": "MongoDB\\Operation\\InsertOne",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/mongodb/laravel-mongodb/src/Collection.php",
"line": 49,
"function": "insertOne",
"class": "MongoDB\\Collection",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/mongodb/laravel-mongodb/src/Query/Builder.php",
"line": 678,
"function": "__call",
"class": "MongoDB\\Laravel\\Collection",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php",
"line": 2026,
"function": "insertGetId",
"class": "MongoDB\\Laravel\\Query\\Builder",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/mongodb/laravel-mongodb/src/Eloquent/Builder.php",
"line": 101,
"function": "__call",
"class": "Illuminate\\Database\\Eloquent\\Builder",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php",
"line": 1338,
"function": "insertGetId",
"class": "MongoDB\\Laravel\\Eloquent\\Builder",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php",
"line": 1303,
"function": "insertAndSetId",
"class": "Illuminate\\Database\\Eloquent\\Model",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php",
"line": 1142,
"function": "performInsert",
"class": "Illuminate\\Database\\Eloquent\\Model",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/mongodb/laravel-mongodb/src/Eloquent/Model.php",
"line": 749,
"function": "save",
"class": "Illuminate\\Database\\Eloquent\\Model",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php",
"line": 1069,
"function": "save",
"class": "MongoDB\\Laravel\\Eloquent\\Model",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Support/helpers.php",
"line": 359,
"function": "Illuminate\\Database\\Eloquent\\{closure}",
"class": "Illuminate\\Database\\Eloquent\\Builder",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php",
"line": 1068,
"function": "tap"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php",
"line": 23,
"function": "create",
"class": "Illuminate\\Database\\Eloquent\\Builder",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php",
"line": 2340,
"function": "forwardCallTo",
"class": "Illuminate\\Database\\Eloquent\\Model",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php",
"line": 2352,
"function": "__call",
"class": "Illuminate\\Database\\Eloquent\\Model",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/app/Imports/AssetsImport.php",
"line": 55,
"function": "__callStatic",
"class": "Illuminate\\Database\\Eloquent\\Model",
"type": "::"
},
{
"file": "/home/anto/smartify/saptest/vendor/maatwebsite/excel/src/Sheet.php",
"line": 268,
"function": "collection",
"class": "App\\Imports\\AssetsImport",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/maatwebsite/excel/src/Reader.php",
"line": 116,
"function": "import",
"class": "Maatwebsite\\Excel\\Sheet",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/mongodb/laravel-mongodb/src/Concerns/ManagesTransactions.php",
"line": 101,
"function": "Maatwebsite\\Excel\\{closure}",
"class": "Maatwebsite\\Excel\\Reader",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/mongodb/mongodb/src/Operation/WithTransaction.php",
"line": 63,
"function": "MongoDB\\Laravel\\Concerns\\{closure}",
"class": "MongoDB\\Laravel\\Connection",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/mongodb/mongodb/src/functions.php",
"line": 537,
"function": "execute",
"class": "MongoDB\\Operation\\WithTransaction",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/mongodb/laravel-mongodb/src/Concerns/ManagesTransactions.php",
"line": 107,
"function": "MongoDB\\with_transaction"
},
{
"file": "/home/anto/smartify/saptest/vendor/maatwebsite/excel/src/Transactions/DbTransactionHandler.php",
"line": 30,
"function": "transaction",
"class": "MongoDB\\Laravel\\Connection",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/maatwebsite/excel/src/Reader.php",
"line": 111,
"function": "__invoke",
"class": "Maatwebsite\\Excel\\Transactions\\DbTransactionHandler",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/maatwebsite/excel/src/Excel.php",
"line": 154,
"function": "read",
"class": "Maatwebsite\\Excel\\Reader",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php",
"line": 357,
"function": "import",
"class": "Maatwebsite\\Excel\\Excel",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/app/Http/Controllers/PagesController.php",
"line": 53,
"function": "__callStatic",
"class": "Illuminate\\Support\\Facades\\Facade",
"type": "::"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Routing/Controller.php",
"line": 54,
"function": "handle_upload",
"class": "App\\Http\\Controllers\\PagesController",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php",
"line": 43,
"function": "callAction",
"class": "Illuminate\\Routing\\Controller",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
"line": 260,
"function": "dispatch",
"class": "Illuminate\\Routing\\ControllerDispatcher",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
"line": 206,
"function": "runController",
"class": "Illuminate\\Routing\\Route",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
"line": 806,
"function": "run",
"class": "Illuminate\\Routing\\Route",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 144,
"function": "Illuminate\\Routing\\{closure}",
"class": "Illuminate\\Routing\\Router",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php",
"line": 50,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Illuminate\\Routing\\Middleware\\SubstituteBindings",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php",
"line": 64,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Illuminate\\Auth\\Middleware\\Authenticate",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php",
"line": 88,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php",
"line": 49,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Illuminate\\View\\Middleware\\ShareErrorsFromSession",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php",
"line": 121,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php",
"line": 64,
"function": "handleStatefulRequest",
"class": "Illuminate\\Session\\Middleware\\StartSession",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Illuminate\\Session\\Middleware\\StartSession",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php",
"line": 37,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php",
"line": 75,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Illuminate\\Cookie\\Middleware\\EncryptCookies",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 119,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
"line": 805,
"function": "then",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
"line": 784,
"function": "runRouteWithinStack",
"class": "Illuminate\\Routing\\Router",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
"line": 748,
"function": "runRoute",
"class": "Illuminate\\Routing\\Router",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
"line": 737,
"function": "dispatchToRoute",
"class": "Illuminate\\Routing\\Router",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
"line": 200,
"function": "dispatch",
"class": "Illuminate\\Routing\\Router",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 144,
"function": "Illuminate\\Foundation\\Http\\{closure}",
"class": "Illuminate\\Foundation\\Http\\Kernel",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
"line": 21,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php",
"line": 31,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
"line": 21,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php",
"line": 51,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\TrimStrings",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Http/Middleware/ValidatePostSize.php",
"line": 27,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Illuminate\\Http\\Middleware\\ValidatePostSize",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php",
"line": 110,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Http/Middleware/HandleCors.php",
"line": 49,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Illuminate\\Http\\Middleware\\HandleCors",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Http/Middleware/TrustProxies.php",
"line": 57,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Illuminate\\Http\\Middleware\\TrustProxies",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 119,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
"line": 175,
"function": "then",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
"line": 144,
"function": "sendRequestThroughRouter",
"class": "Illuminate\\Foundation\\Http\\Kernel",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Foundation/Application.php",
"line": 1168,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Kernel",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/public/index.php",
"line": 17,
"function": "handleRequest",
"class": "Illuminate\\Foundation\\Application",
"type": "->"
},
{
"file": "/home/anto/smartify/saptest/vendor/laravel/framework/src/Illuminate/Foundation/resources/server.php",
"line": 16,
"function": "require_once"
}
]
}
.csv,.xlsand.xlsxfiles