Searchable   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 21
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A scopeSearch() 0 19 4
1
<?php
2
3
use Exception;
4
use Illuminate\Database\Eloquent\Builder;
5
use Illuminate\Support\Str;
6
7
trait Searchable
8
{
9
    public function scopeSearch(Builder $builder, string $term = '')
10
    {
11
        if (! $this->searchable) {
12
            throw new Exception('Please define the searchable property . ');
13
        }
14
        foreach ($this->searchable as $searchable) {
15
            if (str_contains($searchable, '.')) {
16
                $relation = Str::beforeLast($searchable, '.');
17
18
                $column = Str::afterLast($searchable, '.');
19
20
                $builder->orWhereRelation($relation, $column, 'like', "%$term%");
21
22
                continue;
23
            }
24
            $builder->orWhere($searchable, 'like', "%$term%");
25
        }
26
27
        return $builder;
28
    }
29
}
30