Cara menggunakan install mongodb laravel

Setelah proses download package pada MongoDb selesai, selanjutnya buka folder dimana file yang telah didownload tadi berada kemudian mulai proses installasinya dengan cara klik kanan pada package yang telah didownload kemudian pilih install.

Cara menggunakan install mongodb laravel

Kemudian klik tombol Next untuk melanjutkan proses installasinya.

Cara menggunakan install mongodb laravel

Setelah itu scroll user license aggrement sampai bawah kemudian beri centang pada accept the terms in the license aggrement , selanjutnya klik tombol Next untuk melanjutkan ke proses selanjutnya.

Cara menggunakan install mongodb laravel

Pada bagian Setup Type kita akan melakukan installasi semua fiturnya dengan mengeklik tombol Complete. Kalian juga dapat menginstall fitur-fiturnya sesuai dengan kebutuhan kalian dengan menekan tombol custom. Kemudian klik tombol Next untuk melanjutkan ke proses selanjutnya.

Cara menggunakan install mongodb laravel

Pada bagian Service Conviguration pilih radio button Run Service as Network Service User , kemudian klik Next untuk melanjutkan prosesnya.

Cara menggunakan install mongodb laravel

Selanjutnya kalian akan diberi pilihan apakah akan menginstall MongoDB Compass sekalian atau tidak. MongoDB Compass sendiri merupakan tools GUI dari mongoDB yang dibuat oleh mongoDB sendiri. Tools ini hampir mirip phpmyadmin jika di MySQL. Untuk tutorial ini kita juga akan melakukan install MongoDB Compass sekalian sehingga kalian dapat memberikan centang pada Install MongoDB Compass , selanjutnya klik Next untuk melanjutkan ke tahap berikutnya.

Cara menggunakan install mongodb laravel

Selanjutnya klik tombol Install untuk memulai penginstallan paket-paket pada MongoDB ke komputer kalian. Tunggu prosesnya sampai selesai.

Cara menggunakan install mongodb laravel

Terkahir klik finish untuk mengakhiri proses installasinya. Dan proses installasi MongoDB pada komputer windows kalian telah berhasil. Selanjutnya kita akan mempelajari cara penggunaan dari MongoDB .

Sekian artikel dari saya kali ini mengenai Cara install MongoDB di Windows 10 Disertai Gambar , Jangan lupa baca artikel saya yang lain ya…

Laravel’s Eloquent library allows us to map and perform database CRUD operations (Create, Read, Update, Delete) directly from the Laravel models.

Assuming that we have a MongoDB collection called “posts,” an example document in the "posts" collection might look something like this:

{ 
   "title":"First Blog Post",
   "body" :"Lorem Ipsum, etc.",
   "slug" :"first-blog-post"
}

If you are using MongoDB Atlas, you can use the Atlas Console to insert this document record directly into the database named “myappdb.”

Create a Model, Controller, Route, and a View File

Our first step will be to create a Laravel model to represent the blog posts. From the project directory, run the following command:

php artisan make:model Post -mc

This will create an App/Models/Post.php and an App/Http/Controllers/PostController.php file.

By default, artisan creates models that extend Illuminate\Database\Eloquent\Model.

However, for MongoDB, we want to extend the MongoDB Eloquent model, so we want to edit App/Models/Post.php. Our Post model ought to look like this:

namespace App\Models;

use Jenssegers\Mongodb\Eloquent\Model;

class Post extends Model
{
   protected $connection = 'mongodb';

}

Note that when storing new data, Laravel will automatically create the collection in the MongoDB database for you. By default, the collection name is the plural of the model used (“posts” in this case). However, you can override that by setting a collection property on the model like this:

protected $collection = 'blog_posts';

If you were using multiple databases, you would also want to specify the database connection name on the model as indicated above.

Next, we can use our Post model to read and display our blog posts from the MongoDB database. First, let’s create a function in the PostController to get a blog post using a slug:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Post;

class PostController extends Controller
{
   public function show($slug)
   {
       return view('post', [
           'post' => Post::where('slug', '=', $slug)->first()
       ]);
   }

}

In the example above, we are just retrieving the post using its slug name. The MongoDB Eloquent models support all the standard Eloquent query methods, but they also support additional queries that are specific to MongoDB itself. For more details, see https://github.com/jenssegers/laravel-mongodb.

Next, let’s add the following line to the routes\web.php file to create a route for the blog posts:

Route::get('/post/{slug}', [PostController::class, 'show']);
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('/post/{slug}', [PostController::class, 'show']);

Display the Data Using the Laravel View

Finally, let’s add a view to format and style the blog post data for display. The view name needs to match the view that we use in our controller. We can do this by creating the file myapp/resources/views/post.blade.php with the following content:




   MyBlog


   

{{$post->title}}

{{$post->body}}

Now we can see our post at http://localhost:8000/post/first-blog-post.

Save the Data into the MongoDB Database

Now, let’s say you also wanted to create an API that you could use to create blog posts in your application. First, we would want to add a method in our PostController to store a post:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Post;

class PostController extends Controller
{
   public function show($slug)
   {
       return view('post', [
           'post' => Post::where('slug', '=', $slug)->first()
       ]);
   }

   public function store(Request $request)
   {
       $post = new Post;

       $post->title = $request->title;
       $post->body = $request->body;
       $post->slug = $request->slug;

       $post->save();

       return response()->json(["result" => "ok"], 201);
   }

}

Next, we want to configure a resource route for posts in App\routes\api.php:

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

use App\Http\Controllers\PostController;

Route::resource('posts', PostController::class)->only([
   'destroy', 'show', 'store', 'update'
]);

And finally, we can test our API by making a POST request to http://localhost:8000/api/posts. Here’s an example using the Postman app:

Document

php artisan make:model Post -mc
0

Cara menggunakan install mongodb laravel

We can check the MongoDB database and see the blog post has been stored:

Cara menggunakan install mongodb laravel

We should also be able to see the new post by going to http://localhost:8000/post/second-blog-post.

Deleting Data From the Database

As you may have noticed, if a primary key is not specified on the model, the property _id is used as the primary key for each record.

To delete a record, we can use that id to find and delete records. Since we already defined a resource route for posts, we only need to add a destroy() method to the PostController:

php artisan make:model Post -mc
1

Then, we can issue a DELETE request to the API endpoint http://localhost:8000/api/posts/to delete a post.

Updating Data in the Database

We have created APIs to create and delete blog posts. We may also want to create an API for updating blog posts. This is very similar to the previous examples. We just need to add a method to the PostController:

php artisan make:model Post -mc
2

Now, we can issue a PUT request to the API endpoint http://localhost:8000/api/posts/with the updated content of the blog post.