1

I'm trying to use sync on a many to many that includes a status and a comment. I can sync the applications without status and comment just fine.

NewUserAccount Model

public function applications()
{
   return $this->belongsToMany('App\Application', 'new_user_account_applications', 'new_user_id')->withPivot('application_comment', 'status');
}

Application Model

public function newUserAccounts()
{
  return $this->belongsToMany('App\NewUserAccount', 'new_user_accounts_applications', 'new_user_id')->withPivot('application_comment', 'status');
}

My NewUserAccountController

public function store(StoreRequest $request)
    {
       $userAccount = NewUserAccount::create(array_merge(
            $request->all(),
            ['submitted_by' => $requester->id],
            ['start_date' => Carbon::parse($request->input('start_date'))],
            ['account_expires' => $request->accountExpires('newAccountExpireDate')],
            ['company_id' => $requester->company_id],
            ['username' => $request->manuallyAssignId()]
        ));

       // Here I sync applications and include application comment and status 
       $userAccount->applications()->sync($request->applications, ['application_comment' => $request->application_comment, 'status' => 0]);

       ....

    }

My pivot showing status and comment correctly
enter image description here

My form. Here is where I'm not sure how to handle the comment and get it to save with each application pivot record.

@foreach($applications as $application)
    <label class="k-checkbox">
        <input value="{{ $application->id }}" name="applications[]" type="checkbox">{{ $application->application_name }} <span></span> 
    </label>

    <div class="form-group col-lg-4 mb-3">
        <label>Comments</label>
            <textarea name="application_comment[]" class="form-control" rows="2"></textarea>
    </div>
@endforeach
Ben96
  • 521
  • 1
  • 5
  • 18
Northify
  • 351
  • 2
  • 5
  • 21
  • 1
    I am not sure what is the question. Can you ask a clear question? – MEDZ Oct 31 '19 at 16:17
  • I need to save both status and comment in my pivot table. I can only save status with this code. Not sure how I can also save the comment. – Northify Oct 31 '19 at 16:33
  • Possible duplicate of [laravel-sync-how-to-sync-an-array-and-also-pass-additional-pivot-fields](https://stackoverflow.com/questions/27230672/laravel-sync-how-to-sync-an-array-and-also-pass-additional-pivot-fields). – nmfzone Oct 31 '19 at 17:03

1 Answers1

3

First, you need to set the correct index for the application_comment attribute in your textarea. It's needed to correctly determine the comment for each application.

@foreach($applications as $application)
    ...
    <textarea name="application_comment[{{ $application->id }}]" class="form-control" rows="2"></textarea>
    ...
@endforeach

Then, you just need to format your data to:

$userAccount->applications()->sync([
    application_id_1 => ['application_comment' => 'comment for application_id 1'],
    application_id_2 => ['application_comment' => 'comment for application_id 2'],
    ...
]);

So, here it is

$applications = collect($request->applications)->mapWithKeys(function ($appId) use ($request) {
    return [$appId => [
        'application_comment' => $request->input('application_comment')[$appId],
        'status' => 0,
    ]];
});

$userAccount->applications()->sync($applications);
nmfzone
  • 2,755
  • 1
  • 19
  • 32