Laravel保存/更新多对多关系 [英] Laravel save / update many to many relationship

查看:75
本文介绍了Laravel保存/更新多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我如何挽救多对多关系吗?我有任务,用户可以有许多任务,任务可以有许多用户(很多),我要实现的是,在 update形式中,admin可以将多个用户分配给特定任务.这是通过html多项选择输入

Can anyone help me on how to save many to many relationship? I have tasks, user can have many tasks and task can have many users (many to many), What I want to achieve is that in update form admin can assign multiple users to specific task. This is done through html multiple select input

name="taskParticipants[]"

这里的要点是,通过相同的表单(输入),您可以添加/删除用户,这就是为什么我必须使用sync()的原因. 也许我应该从头开始,但不知道从哪里开始...

The catch here is that through the same form (input) you can add/remove users, that's why I have to use sync(). Maybe I should start from the beginning but don't know where to start...

这是我的用户模型:

public function tasks()
{
    return $this->belongsToMany('Task','user_tasks');
}

任务模型

public function taskParticipants()
{
    return $this->belongsToMany('User','user_tasks');
}

TaskController

public function update($task_id)
{
    if (Input::has('taskParticipants'))
    {
        foreach(Input::get('taskParticipants') as $worker)
        {
            $task2 = $task->taskParticipants->toArray();
            $task2 = array_add($task2,$task_id,$worker);
            $task->taskParticipants()->sync(array($task2));
        }
    }
}

这是表格的结构 任务 id | title |最后期限

This is structure of tables tasks id|title|deadline

user_tasks
id|task_id|user_id

推荐答案

tldr; sync与第二个参数false

tldr; Use sync with 2nd param false

在两个模型上,多对多关系为belongsToMany

Many-to-many relationship is belongsToMany on both models:

// Task model
public function users()
{
  return $this->belongsToMany('User', 'user_tasks'); // assuming user_id and task_id as fk
}

// User model
public function tasks()
{
  return $this->belongsToMany('Task', 'user_tasks');
}


要添加新关系,请使用attachsync.

两者之间的差异是:

1 attach将在数据透视表上添加新行,而不检查它是否已经存在.当您具有链接到该关系的其他数据时,这很好,例如:

1 attach will add new row on the pivot table without checking if it's already there. It's good when you have additional data linked to that relation, for example:

UserExam与数据透视表attempts: id, user_id, exam_id, score

我认为这不是您所需要的条件:

I suppose this is not what you need in your situation:

$user->tasks()->getRelatedIds(); // [1,2,3,4,5,6]

$user->tasks()->attach([5,6,7]);
// then
$user->tasks()->getRelatedIds(); // [1,2,3,4,5,6,5,6,7]


另一方面,

2 sync将删除所有关系并重新设置它们:


2 sync on the other hand, will either remove all relations and set them up anew:

$user->tasks()->getRelatedIds(); // [1,2,3,4,5,6]

$user->tasks()->sync([1,2,3]);
// then
$user->tasks()->getRelatedIds(); // [1,2,3]

否则它将建立新的关系而不会分离以前的关系,也不会添加重复项:

or it will setup new relations without detaching previous AND without adding duplicates:

$user->tasks()->sync([5,6,7,8], false); // 2nd param = detach
// then
$user->tasks()->getRelatedIds(); // [1,2,3,4,5,6,7,8]

这篇关于Laravel保存/更新多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆