You are not logged in.

Important Note: This forum is being archived and will be completely locked down on the 7th of February. The new forum can be found at http://laravel.io.

Announcement

    Laravel 4 Advanced Architecture: Book Now On Sale

#1 2014-01-08 07:39:32

Yue_Hong
Artisan
Registered: 2013-09-26
Posts: 9

Laravel 4 seed database table with relationship

Usually we create a database seed file like this to seed a database table:

class UsersSeeder extends DatabaseSeeder {

    public function run()
    {
        $users = [
            [
              'email' => 'mymail@mail.com',
              'password' => Hash::make('123456'),
              'name' => 'Admin'
            ]
       ];

       foreach ($users as $user) {
         User::create($user);
       }
    }

}

But how can we seed table with relationship? For example, table users with table memberdetail:

Users Table:

id, email, password, name

memberdetail table:

id, userid, gender, address, dob

memberdetail table's userid column will show the id which links to the id on users table. How can we seed these 2 tables?

Thank you.

Offline

#2 2014-01-08 08:42:15

the47thsen
Artisan
From: Kobe, Japan
Registered: 2013-10-02
Posts: 77

Re: Laravel 4 seed database table with relationship

You seed it the same way as above.

class memberdetailSeeder extends DatabaseSeeder {
    public function run()
    {
        $memberdetail= [
            [
              'userid' => '1',
              'gender' => 'A',
              'address' => 'B',
              'dob' => 'C'
            ]
       ];
       foreach ($memberdetailas $md) {
         User::create($md);
       }
    }

}

Offline

Board footer

Powered by FluxBB

');