phplaraveldatabaselaravel-5laravel-seeding

Laravel Missing Argument db:seed


I'm using Laravel. I'm creating my database and seed files. (not using Test Dummy) When I start the process of terminal and i got a Missing Argument errors.

khazax@khz ~/Code/Laravel $ php artisan db:seed
Seeded: UsersTableSeeder
Seeded: RolesTableSeeder

[ErrorException]
Missing argument 1 for Illuminate\Database\Eloquent\Builder::where()

khazax@khz ~/Code/Laravel $

DatabaseSeeder.php

<?php

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Seeder;


class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {

        Model::unguard();
        $this->call(UsersTableSeeder::class);
        $this->call(RolesTableSeeder::class);
        $this->call(PermissionsTableSeeder::class);
    }
}

UsersTableSeeder.php

<?php

use App\Models\User;
use Illuminate\Database\Seeder;


class UsersTableSeeder extends Seeder
{
    public function run()
    {
        User::truncate();

        User::create([
            'first_name' => 'Onur',
            'last_name' => 'Kaya',
            'username' => 'Khazax',
            'username_slug' => 'khazax',
            'email' => 'onurkayaes@gmail.com',
            'password' => bcrypt('123'),
            'confirmation_code' => md5(microtime()+env('APP_KEY')),
            'confirmed' => 1,
            'settings' => [
                'gender' => 'Erkek',
                'phone' => '111',
                'adress' => 'Eskisehir/Turkiye',
                'home_page' => 'https://khazadum.com',
                'opt_in_monhtly' => false,
                'opt_in_quartly' => false,
                'opt_in_year' => true
            ]
        ]);

        User::create([
            'first_name' => 'Tab',
            'last_name' => 'Dev',
            'username' => 'Tabdev',
            'username_slug' => 'tabdev',
            'email' => 'info@123.com',
            'password' => bcrypt('123'),
            'confirmation_code' => md5(microtime()+env('APP_KEY')),
            'confirmed' => 1,
            'settings' => [
                'gender' => 'Erkek',
                'phone' => '222',
                'adress' => 'Istanbul/Turkey',
                'home_page' => 'https://tabtusu.com',
                'opt_in_monhtly' => false,
                'opt_in_quartly' => false,
                'opt_in_year' => true
            ]
        ]);
    }
}

RolesTableSeeders.php

<?php

use App\Models\Role;
use App\Models\User;
use App\Models\AssignedRole;
use Illuminate\Database\Seeder;

class RolesTableSeeder extends Seeder
{
    public function run()
    {
        Role::truncate();

        $adminRole = new Role;
        $adminRole->name = 'admin';
        $adminRole->display_name = 'Administrator';
        $adminRole->description = 'Tüm yetkilerin var olduğu kullanıcı grubu';
        $adminRole->is_admin = 1;
        $adminRole->save();

        $userRole = new Role;
        $userRole->name = 'user';
        $userRole->display_name = 'User';
        $userRole->description = 'Sadece site içi işlemleri yapabilen kullanıcı grubu';
        $userRole->is_admin = 0;
        $userRole->save();

        $admin = User::where('email', 'onurkayaes@gmail.com')->first();
        $assRoleAdmin = new AssignedRole;
        $assRoleAdmin->user_id = $admin->id;
        $assRoleAdmin->role_id = $adminRole->id;
        $assRoleAdmin->save();

        $user = User::where('email', 'info@123.com')->first();
        $assRoleUser = new AssignedRole;
        $assRoleUser->user_id = $user->id;
        $assRoleUser->role_id = $userRole->id;
        $assRoleUser->save();

    }
}

and PermissionsTableSeeders.php

<?php


use App\Models\Permission;
use App\Models\Role;
use Illuminate\Database\Seeder;

class PermissionsTableSeeder extends Seeder
{
    public function run()
    {

        Permission::truncate();

        $permission = [

            array(
                'name' => 'manage_user',
                'display_name' => 'Manage Users',
                'is_admin' => 1
            ),

            array(
                'name' => 'manage_content',
                'display_name' => 'Manage Content',
                'is_admin' => 1

            ),

            array(
                'name' => 'manage_roles',
                'display_name' => 'Manage User Roles',
                'is_admin' => 1
            ),

            array(
                'name' => 'manage_files',
                'display_name' => 'Manage Files',
                'is_admin' => 1

            )

        ];

        foreach($permission as $row)
        {
            $row = array_merge($row, ['created_at' => new DateTime, 'updated_at' => new DateTime]);
            DB::table('permissions')->insert($row);
        }

        $role_id_admin = Role::where()->first()->id;
        $perm_base = (int)DB::table('permissions')->first()->id-1;

        $perms = [
            array(
                'role_id' => $role_id_admin,
                'permission_id' => $perm_base + 1
            ),

            array(
                'role_id' => $role_id_admin,
                'permission_id' => $perm_base + 2
            ),

            array(
                'role_id' => $role_id_admin,
                'permission_id' => $perm_base + 3
            ),

            array(
                'role_id' => $role_id_admin,
                'permission_id' => $perm_base + 4
            ),

            array(
                'role_id' => $role_id_admin,
                'permission_id' => $perm_base + 5
            )
        ];

        DB::table('permission_role')->delete();

        foreach($perms as $row)
        {
            $row = array_merge($row, ['created_at' => new DateTime, 'updated_at' => new DateTime]);
            DB::table('permission_role')->insert($row);
        }
    }
}

Am I missing some code or is this just a simple typo? Thanks in advance for helping.


Solution

  • The problem in your code is that line:

    $role_id_admin = Role::where()->first()->id;
    

    in PermissionsTableSeeder.

    Depending on your logic it should look like this:

    $role_id_admin = Role::first()->id;
    

    or you should also add condition for this like this:

    $role_id_admin = Role::where('name','admin')->first()->id;