简体   繁体   中英

How to insert multiple value in pivot table using attach or sync method in Laravel

I'm working with laravel many to many relationship.In my database seed file I'm add the multiple insert in pivot_table using attach() method like -

 $role_admin->permissions()->attach(array($permission_create,$permission_read,$permission_update,$permission_delete));

But when i run the db:seed artisan command it's show this error.

PDOException::("SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '{"id":1,"name":"Create","slug":"Create","description":"This is basic Create Permission","created_at":"2018-12-17 05:23:31","upda' for column 'permission_id' at row 1")

if i'm use sync() instead of attach() then show this error.

ErrorException : Illegal offset type

Now, I want to know how can i multiple value at a time in my permisson_role pivot Table

Here,is my RoleTableSeeder Sample...

class RoleTableSeeder extends Seeder
 {
/**
 * Run the database seeds.
 *
 * @return void
 */
public function run()
{
    $permission_create=Permission::where('slug','Create')->first();
    $permission_read=Permission::where('slug','Read')->first();
    $permission_update=Permission::where('slug','Update')->first();
    $permission_delete=Permission::where('slug','Delete')->first();

    $role_admin=new Role;
    $role_admin->name="Admin";
    $role_admin->slug=ucfirst("admin");
    $role_admin->description="This is Super-Admin Role";
    $role_admin->save();
    $role_admin->permissions()->attach(array($permission_create,$permission_read,$permission_update,$permission_delete));
    //$role_admin->permissions()->sync(array($permission_create,$permission_read,$permission_update,$permission_delete));


}
}

->first()不返回ID,您应该这样使用: ->first()->id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM