-
Notifications
You must be signed in to change notification settings - Fork 554
/
Relation.php
53 lines (42 loc) · 1.36 KB
/
Relation.php
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
48
49
50
51
52
53
<?php
namespace A17\Twill\Services\Listings\Columns;
use A17\Twill\Exceptions\ColumnMissingPropertyException;
use A17\Twill\Models\Contracts\TwillModelContract;
use A17\Twill\Services\Listings\TableColumn;
use Illuminate\Support\Str;
class Relation extends TableColumn
{
private ?string $relation = null;
public function getKey(): string
{
if ($this->key === null) {
throw new ColumnMissingPropertyException();
}
return $this->relation . Str::studly($this->field);
}
public function getSortKey(): string
{
if (null === $this->sortKey) {
return $this->relation . ucfirst($this->field);
}
return $this->sortKey;
}
/**
* Set the relation that should be used.
*/
public function relation(string $relation): static
{
$this->relation = $relation;
return $this;
}
protected function getRenderValue(TwillModelContract $model): string
{
if (null === $this->relation) {
throw new ColumnMissingPropertyException('Relation column missing relation value: ' . $this->field);
}
// @todo: I feel this can be optimized.
/** @var \Illuminate\Database\Eloquent\Collection $relation */
$relation = $model->{$this->relation}()->get();
return $relation->pluck($this->field)->join(', ');
}
}