3
votes

I am running the following code in one of laravel controllers ,

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

However I seem to be getting the following error from the server,

{"error":{"type":"ErrorException","message":"preg_replace(): Parameter mismatch, pattern is a string while replacement is an array","file":"/Users/S/Sites/app-api/vendor/laravel/framework/src/Illuminate/Support/helpers.php","line":885}}

I think it is complaining about the form of what I am sending to the sync as an argument, this is an array that looks looks like this I am assuming this is because I am sending a poorly formatted array?

To build the array I send to the sync I loop of an array that is sent in the POST,

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));
    }
}

I am trying to send an array that contains the key which is the members id, and then I wanting to send some data to the pivot table also, which is a field is_admin and the value can either be 1 or 0.

If I return $members before I run the sync laravel returns some JSON that looks like this,

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

Is this the correct format? Am I doing something wrong?

I am now getting a new error,

Thanks for this, I am now getting a new error,Call to undefined method Illuminate\Database\Query\Builder::sync()

For the sake of clarity here are my relationships,

Organisation

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

User

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

am I wrong in assuming that many-to-many relationships have the sync relationship?

here is the full method,

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

1 Answers

3
votes

Yes you're array has the wrong structure. If you look at the docs (need to scroll a bit down) the id should be the key of the array. Like so:

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

You can do this by a little modification

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

Also you can write it a bit simpler

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