Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Allow passing of additional relationship data #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/MultiSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class MultiSelect extends Field
* @var int
*/
private $maxItems;

/**
* additional attachment data
* @var mixed
*/
private $attachmentData = null;

public function options($value)
{
Expand Down Expand Up @@ -76,6 +82,19 @@ public function maxItems(int $maxItems)

return $this;
}

/**
* Pass additional fields to the relationship for attachement.
*
* @param array $attachmentData
* @return $this
*/
public function attachAdditionalRelationData(array $attachmentData)
{
$this->attachmentData = $attachmentData;

return $this;
}

public function meta()
{
Expand Down Expand Up @@ -118,8 +137,13 @@ protected function fillAttributeFromRequest(NovaRequest $request, $requestAttrib
$relation->detach($currentRelationIds->diff($requestIds)->all());

// figure out whats left, and attach them
$toAttachArray = [];
if (null !== $this->attachmentData) {
$toAttachArray = $this->attachmentData;
}
$relation->attach(
$requestIds->diff($currentRelationIds)->all()
$requestIds->diff($currentRelationIds)->all(),
$toAttachArray
);
};
}
Expand Down