Laravel在包含多个额外字段的多对多上使用同步 [英] Laravel use sync on a many to many that includes a multiple extra fields

查看:68
本文介绍了Laravel在包含多个额外字段的多对多上使用同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对包括状态和评论在内的许多对象使用同步.我可以在没有状态的情况下同步应用程序,也可以发表评论.

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型号

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

Application型号

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

我的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

我的表格.这是我不确定如何处理注释以及如何将其保存到每个应用程序数据透视记录中的地方.

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

推荐答案

首先,您需要在文本区域中为application_comment属性设置正确的索引.需要正确确定每个应用程序的注释.

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'],
    ...
]);

所以,就在这里

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

$userAccount->applications()->sync($applications);

这篇关于Laravel在包含多个额外字段的多对多上使用同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆