Show
IchiiP Posted on Oct 2, 2021 • Updated on Feb 21 Let's operate and display MongoDB from Laravel! Fortunately, drivers with abundant functions are prepared in PHP and Laravel, so it's super easy to implement! Let's do it. Sample ProjectI'll distribute the sample project as usual this time as well. Rename the Sample Code Repository But please read the whole thing and understand how it works, even if you're just "in the mood"... DEV EnvinormentLaravel is an active project and the specifications can change quite a bit depending on the version!
Enter fullscreen mode Exit fullscreen mode Envinorment SettingFirst, create the Laravel Project via Composer.
Enter fullscreen mode Exit fullscreen mode Make MongoDB availableInstall the MongoDB driver for PHP using Pecl.
Enter fullscreen mode Exit fullscreen mode And introduced a library via Composer that allows query builders etc. to be used in the Laravel project.
Enter fullscreen mode Exit fullscreen mode [Note] Make sample DatabaseAs a preliminary step, We will create a sample collection.
Enter fullscreen mode Exit fullscreen mode ※When using MongoDB Compass (CRUD software for GUI), you can create it just by reading the following JSON. Implemented with Laravel!Edit ConfigEdit/add to the following contents in the Config directory.
Enter fullscreen mode Exit fullscreen mode /config/database.php(Edit)
Enter fullscreen mode Exit fullscreen mode Credentials in .env
Enter fullscreen mode Exit fullscreen mode Make ModelNext, we will create the Model, which is "M" in MVC.
Enter fullscreen mode Exit fullscreen mode This will automatically create a Model in /app/Models/Post.php(New)
Enter fullscreen mode Exit fullscreen mode Create a Routingroutes/web.php(Edit)
Enter fullscreen mode Exit fullscreen mode // ↑ When you access /, it will reference the TestController. Create a ControllerWell, that's the point. Create a controller with the following artisan command.
Enter fullscreen mode Exit fullscreen mode Then copy and paste all of the following.
Enter fullscreen mode Exit fullscreen mode Create ViewFinally, create a View and you're done!
Enter fullscreen mode Exit fullscreen mode That's All! Operation CheckStart the build-in server with I'll also post it on the repo on GitHub. Please try it. (GitHub pushes only laravel diff directories/files) https://github.com/ichii731/php-examples/tree/main/laravel_mongodb-show Please also check the blog and Twitter@ichii731 if you like :D Can you use MongoDB with Laravel?Can I Use MongoDB With Laravel? Yes! In fact, MongoDB is a great choice for Laravel projects. As we get started with Laravel development using MongoDB, we'll work through an example of how to build a blog application.
Can I use PHP with MongoDB?You can add the driver to your application to work with MongoDB in PHP. The MongoDB PHP Driver consists of the two following components: The extension , which provides a low-level API and mainly serves to integrate libmongoc and libbson with PHP.
Does Laravel support NoSQL?@NickSurmanidze If MongoDB isn't an option, does Laravel support any other NoSQL by default? Yes, it supports Redis. But Mongodb is also an option as well as Elasticsearch if you install a package. For example for Mongo, moloquent is a good package.
How do I install or enable PHP's MongoDB extension?Installing the MongoDB PHP Library. $ composer require mongodb/mongodb.. Using version ^1.8 for mongodb/mongodb.. ./ composer. ... . Running composer update mongodb/mongodb.. Loading composer repositories with package information.. Updating dependencies.. Lock file operations: 4 installs, 0 updates, 0 removals.. Writing lock file.. |