Skip to content

Commit 2bd4a5a

Browse files
committed
adds ability to use relation methods
1 parent 69be86a commit 2bd4a5a

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/Services/Data/Computors/Method.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace LaravelEnso\Tables\Services\Data\Computors;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Support\Collection;
7+
use Illuminate\Support\Str;
68
use LaravelEnso\Helpers\Services\Obj;
79
use LaravelEnso\Tables\Contracts\ComputesModelColumns;
810

@@ -21,9 +23,17 @@ public static function columns($columns): void
2123
public static function handle(Model $row)
2224
{
2325
foreach (self::$columns as $column) {
24-
$row->{$column->get('name')} = $row->{$column->get('name')}();
26+
$method = self::segments($column->get('name'))->last();
27+
$row->{$column->get('name')} = self::segments($column->get('name'))
28+
->slice(0, -1)->reduce(fn ($value, $segment) => $value->{$segment}, $row)
29+
->{$method}();
2530
}
2631

2732
return $row;
2833
}
34+
35+
private static function segments(string $attribute): Collection
36+
{
37+
return Str::of($attribute)->explode('.');
38+
}
2939
}

0 commit comments

Comments
 (0)