Skip to content

Commit 973b3dd

Browse files
authored
Merge pull request #10 from kettasoft/feature/relation-path-check
Add hasRelationPath method for relation path validation
2 parents 95dffe9 + d817d5e commit 973b3dd

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/Traits/InteractsWithRelationsFiltering.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Kettasoft\Filterable\Traits;
44

5+
use Illuminate\Support\Arr;
6+
57
trait InteractsWithRelationsFiltering
68
{
79
/**
@@ -55,6 +57,32 @@ public function getRelations(): array
5557
return $this->relations;
5658
}
5759

60+
/**
61+
* Check if the given path is a valid relation path.
62+
*
63+
* @param string $path
64+
* @return bool
65+
*/
66+
public function hasRelationPath(string $path)
67+
{
68+
if (str_contains($path, '.')) {
69+
70+
$relations = explode('.', $path);
71+
72+
$field = array_pop($relations);
73+
74+
$path = implode('.', $relations);
75+
76+
if (Arr::isAssoc($this->relations)) {
77+
return isset($this->relations[$path]) && in_array($field, $this->relations[$path]);
78+
}
79+
80+
return in_array($relations[0], $this->relations);
81+
}
82+
83+
return false;
84+
}
85+
5886
/**
5987
* Create Filterable instance with define relations attributes.
6088
* @param array $relations

0 commit comments

Comments
 (0)