Laravel-MongoDB CRUD

Create Model

by Remy Pereira on 10th October 2016

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
	{
	    //
	}

Post a comment

Comments

Nothing yet..be the first to share wisdom.