Features of Laravel 9 : What is new in Laravel 9
Laravel Generate PDF Example | Download And Generate PDF In Laravel In Hindi
Laravel MySQL Best Pratice : Laravel Tips And Tricks
Laravel echo, socket io and redis with example : Laravel socket io with redis
PHP Error Control Operator In Hindi | Error Control In PHP
Web Performance In Hindi : Website Performance Kya Hai?
PHP stdClass In Hindi | stdClass In PHP
If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
इस tutorial में आप सीखेंगे कि Laravel Application में MongoDB database कैसे setup करना है , और किस तरह से MongoDB work करता है।
सबसे पहले एक fresh Laravel Application create करते हैं। मैं Laravel version 8 install कर रहा हूँ।
composer create-project --prefer-dist laravel/laravel:8.* laravelmongo
Laravel में MongoDB के साथ work करने के लिए सबसे पहली चीज है package को install करना। इसलिए सबसे jenssegers/mongodb package को install करेंगे।
composer require jenssegers/mongodb ^3.8
यह command , दो package install करेगी, पहला jenssegers/mongodb और दूसरा mongodb जिए आप vendor folder में check कर सकते हैं।
Next , हमें MongoDB database credentials set करने हैं , तो सबसे पहले .env file open करें और नीचे दिया गया code replace कर दें।
//File .env ------------------------ DB_CONNECTION=mongodb DB_HOST= DB_PORT= DB_DATABASE= DB_USERNAME= DB_PASSWORD=
यहां पर हमने सिर्फ driver name change किया है , क्योंकि हम MongoDB database को dsn URI के साथ connect करेंगे।
अब config/database.php file open करें और connections Array में नीचे दिया गया code insert कर दें।
//File : config/connections.php ------------------------ 'mongodb' => [ 'driver' => 'mongodb', 'dsn' => "mongodb+srv://username:password@hostname/dbname?retryWrites=true&w=majority", ]
जरूरी नहीं कि , आप database को dsn से connect करें , अगर URL नहीं है तो आप username , pass का use भी कर सकते हैं। इसके लिए सबसे पहले .env file में username , password define करें फिर उसके बाद config/connections.php file में connections Array में नीचे दिया गया code में रख दे।
config/database.php ---------------------------- 'mongodb' => [ 'driver' => 'mongodb', 'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', 27017), 'database' => env('DB_DATABASE'), 'username' => env('DB_USERNAME'), 'password' => env('DB_PASSWORD'), 'options' => [ 'database' => 'dbname' ] ],
That's It, Database setup हो गया है अब एक controller और model prepare करते हैं , जिससे हम data insert / retrieve कर सके।
अब , testing के लिए एक PostController बनाते हैं।
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class PostController extends Controller
{
public function posts() {
$posts = DB::table('posts')->get();
dd($posts);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Jenssegers\Mongodb\Eloquent\Model;
class Post extends Model
{
use HasFactory;
// define collection / table name
protected $collection = "posts";
// define fillable fields
protected $fillable = [
'title', 'description'
];
}
finally , factories की help से dummy data insert करते हैं।
php artisan make:factory PostFactory --model=Post
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\Post;
class PostFactory extends Factory
{
/**
* The name of the factory's corresponding model.
* @var string
*/
protected $model = Post::class;
/**
* Define the model's default state.
* @return array
*/
public function definition() {
return [
'title' => $this->faker->title,
'description' => $this->faker->text,
];
}
}
इसके बाद नीचे दी गयी command run करें और database में check करेंगे तो 20 records insert हो जायगे।
1. composer dump-autoload 2. php artisan tinker 3. Post::factory()->count(20)->create()
Loading ...
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook