<?php

namespace App\Traits;

use Illuminate\Database\Eloquent\Builder;

trait Searchable
{
    /**
     * Apply a search scope to the query.
     *
     * @param Builder $query
     * @param string|null $keyword
     * @param array|null $columns
     * @return Builder
     */
    public function scopeSearch(Builder $query, ?string $keyword, ?array $columns = null): Builder
    {
        if (!$keyword) {
            return $query;
        }

        $columns = $columns ?? $this->searchableColumns;

        return $query->where(function ($q) use ($keyword, $columns) {
            $q->whereAny($columns, 'LIKE', "%{$keyword}%");
        });
    }
}
