Posts

Showing posts from June, 2018

Laravel with MongoDB CURD operations - part 3

You have already save data to post collection on mongodb. Now we are going to display all the post in your web page. We have already created the route /post to display all posts. You need to use index method in your PostController to write a method to display all posts. Now add following code to get all posts and display them in the view.     /**      * Display a listing of the resource.      *      * @return \Illuminate\Http\Response      */     public function index()     {         $posts = Post::all();         return view('posts.index')->with('posts', $posts);     } If you go to {YOUR_BASE_URL}/posts you will get an error as you haven't add index view yet. Now will add the view. Go to resources/views/posts and add a file "index.blade.php" Add code like following to display your all posts title. <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale

Laravel with MongoDB CURD operations - part 2

Image
We will create simple page with posts. This is our post structure. PostController Post model inside views/posts folder     index.blade     add.blade     edit.blade Create controller using following command. php artisan make:controller PostController -- resource Then you can get PostController in your controller folder with method for show,add,edit, delete and update.Open that controller we are going to add some data to mongodb collection. Inside your PostController fill the create method by returning the add view. Your method now looks like this.     /**      * Show the form for creating a new resource.      *      * @return \Illuminate\Http\Response      */     public function create()     {         return view('posts.add');     } Now you need to add route for view add add page. Go to routes/web.php file and add the following route. Route::resource('posts', 'PostController'); We added all the resource routes at once.Now we h

Laravel with MongoDB CURD operations - part 1

First you need to install Laravel and MongoDB. I assume you have already know how to install Laravel. Will start from MongoDb install. Follow the mongodb official documentation and install MongoDB according to your OS https://docs.mongodb.com/manual/installation/ We need to add MongoDB extention to php next. For do that add the following line to php.ini file extension=php_mongodb.dll Restart the server. Now we are ready to start with Laravel. First you need to give the information for your .env file DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=yourdatabasename Add this on your config/database.php file 'mongodb' => [             'driver'   => 'mongodb',             'host'     => env('DB_HOST', 'localhost'),             'port'     => 27017,             'database' => env('DB_DATABASE', 'test'),           ], You can add username and password.This post I skip the adding