T
T
Tarasovych2017-05-01 13:37:02
MySQL
Tarasovych, 2017-05-01 13:37:02

Adding initial data with migrations?

I usephp artisan db:seed

User::create(array(
            'name' => 'admin',
            'email' => '[email protected]',
            'password' => bcrypt('adminadmin'),
        ));

Role::create(array(
            'name' => 'superadmin',
            'display_name' => 'superadmin',
            'description' => 'superadmin',
        ));

The database has a table role_userthat links the role and the user (fields role_idand user_id). How after INSERT'a to connect the user and a role?
I suppose you need to get the id of the last record in the table usersand rolesthen add these id to the table role_user?
$user = User::create(array(
            'name' => 'admin',
            'email' => '[email protected]',
            'password' => bcrypt('adminadmin'),
        ));

$user_id = $user->id;
...
аналогично с ролью
...
$user_id = 'user_id' => 'data';
$role_id = 'role_id' => 'data';
DB::table('role_user')->insert($data);

p.s.
spoiler
Есть аналог var_dump для консоли?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mokhirjon Naimov, 2017-05-01
@zvermafia

You just need to read the section Eloquent: Relationships : Many To Many , Attaching / Detaching .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question