Skip to content

Commit

Permalink
Feature
Browse files Browse the repository at this point in the history
Allowed access to items before attaching/detaching/syncing
  • Loading branch information
ezra-obiwale committed Jan 17, 2018
1 parent b799aa6 commit 35639f8
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/Controllers/Traits/Attachable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Response;
use Log;
use Illuminate\Database\Eloquent\Model;

/**
* Shortcuts for many-to-many attachments
Expand Down Expand Up @@ -47,6 +48,16 @@ private function treatRelation($model, &$relation) {
}
}

protected function prepareAttachItems($items, Model $model, $relation) {
return $items;
}
protected function prepareDetachItems($items, Model $model, $relation) {
return $items;
}
protected function prepareSyncItems($items, Model $model, $relation) {
return $items;
}

/**
* Fetches a paginated list of related items
*
Expand Down Expand Up @@ -87,7 +98,7 @@ public function attach($id, $relation, $paramKey = null)
try {
$items = request()->input($paramKey);
$this->treatRelation($model, $relation);
$model->$relation()->syncWithoutDetaching($items);
$model->$relation()->syncWithoutDetaching($this->prepareAttachItems($items, $model, $relation));
return response()->json([
'status' => 'ok'
]);
Expand Down Expand Up @@ -121,7 +132,7 @@ public function detach($id, $relation, $paramKey = null)
try {
$items = request()->input($paramKey);
$this->treatRelation($model, $relation);
$model->$relation()->detach($items);
$model->$relation()->detach($this->prepareDetachItems($items, $model, $relation));
return response()->json([
'status' => 'ok'
]);
Expand Down Expand Up @@ -155,7 +166,7 @@ public function sync($id, $relation, $paramKey = null)
try {
$items = request()->input($paramKey);
$this->treatRelation($model, $relation);
$resp = $model->$relation()->sync($items);
$resp = $model->$relation()->sync($this->prepareSyncItems($items, $model, $relation));
$resp['added'] = $resp['attached'];
$resp['removed'] = $resp['detached'];
unset($resp['attached']);
Expand Down

0 comments on commit 35639f8

Please sign in to comment.