ProjectFilter.php 1.61 KB
<?php

namespace App\ModelFilters;

use App\Support\ModelFilter;
use Arr;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Builder as Query;

class ProjectFilter extends ModelFilter
{
    public $relations = [
        'master' => ['masterName' => 'nickName']
    ];

    /**
     * @param string $name
     * @return void
     */
    public function name(string $name): void
    {
        $this->whereLike('name', $name);
    }

    /**
     * @param int|array $status
     * @return void
     */
    public function status(int|array $status): void
    {
        $this->whereIn('status', Arr::wrap($status));
    }

    /**
     * @param int|array $status
     * @return void
     */
    public function isPromote(int|array $status): void
    {
        $this->whereIn('is_promote', Arr::wrap($status));
    }

    /**
     * @param int|array $status
     * @return void
     */
    public function isCanApply(int|array $status): void
    {
        $this->whereIn('is_can_apply', Arr::wrap($status));
    }

    public function masterOrManager(array|int $userId): void
    {
        $this->where(fn(Builder $query) => $query->whereIn('master_id', Arr::wrap($userId))
            ->orWhereExists(fn(Query $query) => $query->selectRaw(1)->from('user_has_projects')->whereColumn('user_has_projects.project_id', 'projects.id')->whereIn('user_has_projects.user_id', Arr::wrap($userId)))
        );
    }

    /**
     * @param $manage
     * @return void
     */
    public function manager($manage): void
    {
        $this->whereRelation('managers', fn(Builder $query) => $query->whereIn('users.id', Arr::wrap($manage)));
    }
}