/** * @param RepositoryInterface $repository * @param NamedColumnInterface $column * @param Builder $query * @param string $search * @param array|string $fullSearch * @param string $operator * * @return void */ public function apply(RepositoryInterface $repository, NamedColumnInterface $column, Builder $query, $search, $fullSearch, $operator = '=') { if (empty($search)) { return; } if ($operator == 'like') { $search = '%' . $search . '%'; } $name = $column->getName(); if ($repository->hasColumn($name)) { $query->where($name, $operator, $search); } elseif (strpos($name, '.') !== false) { $parts = explode('.', $name); $fieldName = array_pop($parts); $relationName = implode('.', $parts); $query->whereHas($relationName, function ($q) use($search, $fieldName, $operator) { $q->where($fieldName, $operator, $search); }); } }
public function initialize() { $this->repository = new BaseRepository($this->getClass()); $this->repository->setWith($this->getWith()); $this->initializeFilters(); foreach ($this->getAllColumns() as $column) { if ($column instanceof ColumnInterface) { $column->initialize(); } } $this->setAttribute('class', 'table table-striped'); }
/** * @param RepositoryInterface $repository * @param NamedColumnInterface $column * @param Builder $query * @param string $search * @param string $fullSearch * @param string $operator * * @return void */ public function apply(RepositoryInterface $repository, NamedColumnInterface $column, Builder $query, $search, $fullSearch, $operator = '=') { if (empty($search)) { return; } try { $time = Carbon::createFromFormat($this->getFormat(), $search); } catch (Exception $e) { try { $time = Carbon::parse($search); } catch (Exception $e) { return; } } $time = $time->format($this->getSearchFormat()); $name = $column->getName(); if ($repository->hasColumn($name)) { $query->where($name, $operator, $time); } elseif (strpos($name, '.') !== false) { $parts = explode('.', $name); $fieldName = array_pop($parts); $relationName = implode('.', $parts); $query->whereHas($relationName, function ($q) use($time, $fieldName, $operator) { $q->where($fieldName, $operator, $time); }); } }