laravel同步多对多关系

时间:2014-12-16 15:11:39

标签: php laravel relational-database eloquent pivot-table

我在一个laravel控制器中运行以下代码,

$organisation->users()->sync($members);

但是我似乎从服务器收到以下错误,

  

{" error":{" type":" ErrorException"," message":" preg_replace():参数不匹配,模式是一个字符串,而替换是一个数组"," file":" / Users / S / Sites / app-api / vendor / laravel / framework / src / Illuminate / Support /helpers.php","线":885}}

我认为它是抱怨我发送到同步的形式作为参数,这是一个看起来像这样的数组我假设这是因为我发送格式不佳的数组?

要构建数组,我将发送到POST中发送的数组的同步I循环

foreach($postData['members'] as $member) {
    if($member['admin'] == 1) {
        $members[] = array($member['id'] => array('is_admin' => 1));
    } else {
        $members[] = array($member['id'] => array('is_admin' => 0));
    }
}

我正在尝试发送一个包含成员id的密钥的数组,然后我想将一些数据也发送到数据透视表,这是一个字段is_admin,其值可以是1或者0。

如果我在运行sync laravel之前返回$members,则返回一些看起来像这样的JSON,

[
    {
        "1":{"is_admin":1}
    },
    {
        "85":{"is_admin":0}
    },
    {
        "86":{"is_admin":0}
    }
 ]

这是正确的格式吗?我做错了吗?

我现在收到一个新错误,

  

感谢您,我现在收到一个新错误,调用未定义的方法Illuminate \ Database \ Query \ Builder :: sync()

为了清楚起见,这是我的关系,

组织

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

用户

public function organisations()
{
    return $this->belongsToMany('Organisation')->withPivot('is_admin');
}
假设多对多关系具有同步关系,我错了吗?

这是完整的方法,

public function update($id)
{   
    //eturn Input::all('name');
    $postData = Input::all();
    $organisation = Organisation::find($id);

    if(!empty($postData['group_name'])) {
        $organisation->name = $postData['group_name'];
    } else {
        $organisation->name = $postData['name'];
    }

    $organisation->information = $postData['information'];
    $organisation->type = $postData['type'];
    $organisation->slug = $this->createSlug($postData['name']);
    //Sort out the members are they admins etc.
    if(isset($postData['members'])) {

        $members = array();

        foreach($postData['members'] as $member) {
            $members[$member['id']] = array('is_admin' => $member['admin']);
        }
    }

    if(isset($postData['clients'])) {

        $clients = array();
        foreach($postData['clients'] as $client) {
            $clients[] = $client['id'];
        }
    }

    if(isset($postData['projects'])) {

        $projects = array();

        foreach($postData['projects'] as $project) {
            $projects[] = $project['id'];
        }
    }

    if( $organisation->save() ) {

        if(isset($members)) {
            $organisation->users()->sync($members);
        }

        if(isset($clients)) {
            $organisation->clients()->sync($clients);
        }

        if(isset($projects)) {
            $organisation->projects()->sync($projects);
        }

        $organisation->load('users');
        $organisation->load('teams');
        $organisation->load('clients');
        $organisation->load('projects');

        return Response::make("success", 200);

    } else {

        return Response::make("Something has gone wrong", 500);

    }

}

1 个答案:

答案 0 :(得分:1)

是的,你的阵列结构错误。如果查看docs(需要向下滚动),id应该是数组的键。像这样:

array(
    '1' => array('is_admin' => 1),
    '85' => array('is_admin' => 0),
    '86' => array('is_admin' => 0),
);

你可以通过一点修改

来做到这一点
foreach($postData['members'] as $member) {
    if($member['admin'] == 1) {
        $members[$member['id']] = array('is_admin' => 1);
    } else {
        $members[$member['id']] = array('is_admin' => 0);
    }
}

你也可以写一点简单

foreach($postData['members'] as $member) {
    $members[$member['id']] = array('is_admin' => $member['admin']);
}