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

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

问题描述

谁能帮助我如何挽救多对多关系?我有任务,用户可以有很多任务,任务可以有很多用户(多对多),我想要实现的是在更新表单中管理员可以将多个用户分配给特定的任务.这是通过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');
}

任务控制器

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; Use sync with 2nd param 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

User and Exam linked with pivot table 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天全站免登陆