<?php

namespace App\Traits;

use Illuminate\Database\Eloquent\Builder;

trait ModCPSearch
{
    protected function applySearch(Builder $query, array $columns = ['name'] ): Builder
    {
        $search = request('s', '');
        if( !$search )
            return $query;

        return $query->where(function ($query) use ($columns, $search) {
            foreach ($columns as $i => $column) {
                $method = $i === 0 ? 'where' : 'orWhere';
                $query->{$method}($column, 'LIKE', "%{$search}%");
            }
        });
    }

}
