-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathHasCategories.php
More file actions
47 lines (40 loc) · 1.29 KB
/
HasCategories.php
File metadata and controls
47 lines (40 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
namespace ProcessMaker\Traits;
use App\Events\Relations\Attached;
use App\Events\Relations\Detached;
use App\Events\Relations\Syncing;
use Illuminate\Database\Eloquent\Relations\Pivot;
use ProcessMaker\Models\CategoryAssignment;
trait HasCategories
{
public function assignable()
{
return $this->morphedByMany(CategoryAssignment::class, 'assignable');
}
public function categories()
{
$categories = $this->morphedByMany(static::categoryClass, 'category', 'category_assignments', 'assignable_id', 'category_id');
$categories->withPivotValue('assignable_type', static::class);
return $categories->using(CategoryAssignment::class);
}
/**
* Set multiple|single categories to the assignable
*
* @param string $value
*/
private function setMultipleCategories($value, $singleColumn)
{
if ($value) {
$value = explode(',', $value);
$this->attributes[$singleColumn] = $value[0];
self::saved(function ($model) use ($value) {
if ($model->getKey() === $this->getKey()) {
$this->categories()->sync($value);
}
});
} else {
$this->attributes[$singleColumn] = $value;
}
return $this;
}
}