app\Product::scopeLike PHP Method

scopeLike() public method

Products tags filter.
public scopeLike ( $query, $attr = [], $search = [] )
    public function scopeLike($query, $attr = [], $search = [])
    {
        //if the search contains a string of words, we split them in an array
        if (!is_array($search)) {
            $search = explode(' ', preg_replace('/\\s+/', ' ', trim($search)));
        }
        $needle = '(';
        if (!is_array($attr)) {
            $attr = [$attr];
        }
        foreach ($attr as $key) {
            foreach ($search as $word) {
                if (trim($word) != '') {
                    $needle .= $key . " like '%" . $word . "%' or ";
                }
            }
        }
        $needle = rtrim($needle, ' or ') . ')';
        $query->whereRaw($needle);
        return $query;
    }