Laravel-MongoDB CRUD

Create Model

Laravel's Eloquent ORM provides mapping between database resources and models. In relational database, models correspond to tables. In MongoDB, models correspond to collections. The MongoDB package (jenssegers/laravel-mongodb) provides Moloquent ORM which is an extension of Eloquent. We can use Moloquent to create models.

Usually, there is a model corresponding to every collection.The name of our collection is books(plural). So we create a corresponding model Book(singular), following the Laravel naming convention. These conventions are not strict, but its best to adhere to them to avoid confusion. You may create the model file manually, or using the artisan CLI.

php artisan make:model Book

This creates model file app/book.php

<?php
	
	namespace App;
		
	class Book extends Eloquent
	{
	    //
	}

Add the following lines to your model.

<?php

	namespace App;

	use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

	class Book extends Eloquent
	{
	    //
	}

Last Updated: 10-October-2016


Post a comment

Name

Your Comment

Email (We dont publish it)

Comments

Nothing yet..be the first to share wisdom.