0
votes

I am doing a project with Laravel framework.I use the 'mongodb' as the database.I created a database seeder file for 'user table ' of the database.After using the'php artisan db:seed' commands it shows that 'Database seeding completed successfully.'.But when I use ' db.users.find()' command it does not give any collection data

Userseeder.php

<?php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //
        DB::table('users')->insert([
            'name' => Str::random(10),
            'email' => Str::random(10).'@gmail.com',
            'password' => Hash::make('password'),
        ]);
    }
}

database.php

'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'url' => env('DATABASE_URL'),
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
            'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
        ],

        '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' => 'admin' // sets the authentication database required by mongo 3
    ]
],

.env

DB_CONNECTION=mongodb
DB_HOST=127.0.0.1
DB_PORT=27017
DB_DATABASE=UserManagementComponent
DB_USERNAME=
DB_PASSWORD=

User.php

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}
1
As far as I know Laravel doesn't support MongoDB as a database driver out of the box. You'd need an additional library to add some sort of support like e.g. jenssegers/laravel-mongodb - apokryfos
I already added it to the app.php providers. - Mihiran Chathuranga

1 Answers

0
votes

Since you are using jenssegers/laravel-mongodb you need to extend Jenssegers\Mongodb\Eloquent\Model instead of Eloquent\Model. In your case specifically you need to extend Jenssegers\Mongodb\Auth\User

use Illuminate\Contracts\Auth\MustVerifyEmail;
use  Jenssegers\Mongodb\Auth\User as Authenticatable; // Here
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
...
}

However this is a bit more complicated because some of the standard Laravel traits that ship with eloquent may not fully work on mongoDB so I suggest you refer to the documentation for more information.