简体   繁体   English

自动连接到Laravel 5中的数据透视表

[英]Automaticlly attach to pivot table in Laravel 5

I currently have a Users to Groups Relationship (ManyToMany) with a pivot table group_user. 我目前有一个具有数据透视表group_user的用户与组的关系(ManyToMany)。 I want the user to be able to create a group but once creating the group, how do I make it, that the creator becomes member of this group? 我希望用户能够创建一个组,但是一旦创建了该组,如何使创建者成为该组的成员呢?

Currently I have 目前我有

My Pivot Table (group_user): 我的数据透视表(group_user):

Schema::create('group_user', function(Blueprint $table)
        {
            $table->integer('group_id')->unsigned()->index();
            $table->foreign('group_id')->references('id')->on('groups')->onDelete('cascade');

            $table->integer('user_id')->unsigned()->index();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

            $table->timestamps();
        });

My Groups table (groups): 我的群组表格(群组):

Schema::create('groups', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        }); 

My Users table (users): 我的用户表(用户):

Schema::create('users', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('username')->unique();
            $table->string('email')->unique();
            $table->string('name');
            $table->string('lastname');
            $table->string('password', 60);
            $table->rememberToken();
            $table->timestamps();
        });

My models ofcourse have the following: User.php 我的课程模型如下:User.php

public function groups() 
    {
        return $this->belongsToMany('App\Group');
    }

Group.php Group.php

public function users()
    {
        return $this->belongsToMany('App\User');
    }

What create function should I write in my controller so that when a User creates a Group, that he automaticly becomes member of this group (automaticlly make the pivot relationship)? 我应该在控制器中编写什么创建函数,以便当用户创建组时,他自动成为该组的成员(自动建立支点关系)?

See attach() and detach(). 请参阅attach()和detach()。

$user = User::find(1);
$user->groups()->attach(10); // pivot relationship of this user to group of id 1.

OR 要么

$group = Group::find(10);
$user->groups()->save($group); 

For many groups of this user: 对于此用户的许多组:

$user->groups()->sync(array(1, 2, 3));

This should work, make sure you implement validation, ect. 这应该起作用,请确保您实施验证等。

public function store(Request $request)
    {
        $group = Group::create([ // <-- if names are unique. if not, then create is fine
        'name' => $request->get('name')
        ]);
        auth()->user()->groups()->attach([$group->id]);

        return view('your.view');

    }

Also make sure to add: 还请确保添加:

use App\Group;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数据透视表附加到laravel中的其他两个表 - attach pivot table to other two tables in laravel 如何将数据透视表附加到具有多个值的表 [LARAVEL] - How to attach pivot to table with multiple values [LARAVEL] 如何在数据透视表Laravel 5中为附加字段附加不同的值 - How to attach different value for additional field in pivot table Laravel 5 Laravel 5.0 在附加后获取插入到数据透视表中的行的 id - Laravel 5.0 get the id of the row inserted in a pivot table after attach Laravel使用attach在数据透视表中包含一个附加字段 - Laravel use attach to include an additional field in pivot table 使用附件保存到数据透视表时,Laravel模型更改器不起作用 - Laravel model mutator not working when using attach to save on pivot table 如何使用附加 function 将用户 ID 和角色 ID 附加到 laravel 中的 pivot 表? - How can I attach the user id and role id to the pivot table in laravel using attach function? Laravel附加数据数组以进行透视 - Laravel attach arrays of data to pivot Laravel 5.4在插入数据透视表时在null上调用成员函数attach() - Laravel 5.4 Call to a member function attach() on null while inserting in pivot table Laravel ManyToMany关系并在没有数据透视表的情况下从输入字段数组附加 - Laravel ManyToMany relationships and attach without pivot table from input field array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM