mardi 29 août 2017

Laravel: how to run seeder with relationships

Why "relationship" in my seeder code isn't working? It inserts to User model without any problem. But doesn't insert anything to UserMeta model. There is not even any error.
I have tested this code in controller. It was functioning fine with "relationship"

    <?php

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

class MyUsersTableSeeder extends Seeder
{
    /**
     * Auto generated seed file.
     *
     * @return void
     */
    public function run()
    {
        if (User::count() == 0) {

            $user = User::create([
                'name'           => 'Admin',
                'email'          => 'admin@admin.com',
                'password'       => bcrypt('password'),
                'remember_token' => str_random(60),
            ]);

            $user_meta = new UserMeta([
                'meta_key' => 'role',
                'meta_value' => 'admin',
            ]);
            $user->UserMeta()->save($user_meta);
        }
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire