Issues (52)

src/Models/Model.php (1 issue)

1
<?php
2
3
namespace Turahe\Master\Models;
4
5
use Illuminate\Database\Eloquent\Model as BaseModel;
6
use Turahe\Master\Traits\AutoFilter;
7
use Turahe\Master\Traits\AutoSort;
8
9
/**
10
 * Class Model.
11
 */
12
abstract class Model extends BaseModel
13
{
14
    use AutoFilter;
15
    use AutoSort;
16
    /**
17
     * @var string
18
     */
19
    protected $keyType = 'string';
20
21
    /**
22
     * @var string[]
23
     */
24
    protected $searchableColumns = ['id', 'name'];
25
26
    /**
27
     * Model constructor.
28
     *
29
     * @param array $attributes
30
     */
31
    public function __construct(array $attributes = [])
32
    {
33
        parent::__construct($attributes);
34
    }
35
36
    /**
37
     * @param $query
38
     * @param $keyword
39
     */
40
    public function scopeSearch($query, $keyword)
41
    {
42
        if ($keyword && $this->searchableColumns) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->searchableColumns of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
43
            $query->whereLike($this->searchableColumns, $keyword);
44
        }
45
    }
46
}
47