0

I'm building a small application on Laravel 5.4and facing a little difficulty in syncing the pivot table in many to many relationship, I went through this link and not understood properly,

I'm having a pivot table which is mandatory field(I mean it will have field). I'm having a relationship something like this:

class Interaction extends Model
{

    public function clientsAssociation()
    {
        return $this->belongsToMany('App\Contact', 'contact_client_interaction',  'interaction_id', 'contact_id')->withPivot('company_id')->withTimestamps();
    }

}

I'm getting an array with set of values related to sync to these models. I'm confused how to place pivot data and while updating:

foreach ($data['clientParticipants'] as $clientParticipant)
{
    if(array_key_exists('company_id', $clientParticipant))
    {
        $contact[] = Contact::find(json_encode($clientParticipant['value']));
        $pivotData = ['company_id' => $clientParticipant['company_id']];
    }
    else
    {
        $contact[] = Contact::find(json_encode($clientParticipant['value']));
        $pivotData = ['company_id' => Contact::find(json_encode($clientParticipant['value']))->company()->withPivot('created_at')->orderBy('pivot_created_at', 'desc')->first()->id])];
    }

    $interaction->clientsAssociation()->sync($contact);
}

Guide me to achieve this. Thanks

zt1983811
  • 1,011
  • 3
  • 14
  • 34
Nitish Kumar
  • 6,054
  • 21
  • 82
  • 148
  • Isn't $pivotData what you want to be passing to sync()? –  Jun 15 '17 at 20:14
  • @btl Yes I want a way out to pass `$pivotData` into sync – Nitish Kumar Jun 15 '17 at 20:17
  • You're close it looks like, use $clientParticipant['company_id'] as the key for $clientData and push it on the array. Then you can sync all the data at once. See the answer here: https://stackoverflow.com/questions/27230672/laravel-sync-how-to-sync-an-array-and-also-pass-additional-pivot-fields?noredirect=1&lq=1 –  Jun 15 '17 at 20:20
  • @btl this link I've already included in the question. I want to know how to make that array. I mean I'm bit confused with the solution. – Nitish Kumar Jun 16 '17 at 02:28

1 Answers1

2

Well I did something like this:

foreach ($data['clientParticipants'] as $clientParticipant)
{
    if(array_key_exists('company_id', $clientParticipant))
    {
        $pivotData = ['company_id' => $clientParticipant['company_id']];
        $syncData = Contact::find(json_encode($clientParticipant['value']))->id;
        $contact[$syncData] = $pivotData;
    }
    else
    {
        $value = Contact::find(json_encode($clientParticipant['value']));
        $syncData = $value->id;
        $pivotData = ['company_id' => $value->company()->withPivot('created_at')->orderBy('pivot_created_at', 'desc')->first()->id];
        $contact[$syncData] = $pivotData;
    }
}
$interaction->clientsAssociation()->sync($contact);

And it works. So basically idea is to push an array with key of pivot elements and it will execute properly.

Nitish Kumar
  • 6,054
  • 21
  • 82
  • 148