getModel()->getTable(); $query = $query->where(function ($q) use ($field, $search, $table) { foreach ($field as $f) { $f = str_replace('?', '\?', $f); if (is_array($f)) { $q->orWhereRaw( sprintf("UPPER(CONCAT(%s)) LIKE UPPER(?)", collect($f)->map(function ($q) use ($table) { return "`$table`.`$q`"; })->implode(', \' \', ')), ["%$search%"] ); } else { $q->orWhereRaw("UPPER(`$table`.`$f`) LIKE UPPER(?)", ["%$search%"]); } } }); } return $query; } /** * Sort columns with a query * * @param Builder $query * @param array $ignore * @return Builder */ protected function sortQuery(Builder $query, array $ignore = []): Builder { $rName = request()->route()->getName(); $table = $query->getModel()->getTable(); if (request()->sort_col) { Session::put("$rName.sort_col", request()->sort_col); } if (request()->sort_way) { Session::put("$rName.sort_way", request()->sort_way); } if (request()->rst) { Session::remove("$rName.sort_col"); Session::remove("$rName.sort_way"); } if (request()->sort_col and request()->sort_way and Schema::hasColumn($table, request()->sort_col)) { if ( !isset($ignore) or (isset($ignore) and !in_array(request()->sort_col, $ignore)) ) { $query = $query->orderBy(\sprintf("$table.%s", request()->sort_col), request()->sort_way); } } elseif (Schema::hasColumn($table, 'order')) { $query = $query->orderBy('order', 'ASC'); Session::put("$rName.sort_col", 'order'); Session::put("$rName.sort_way", 'asc'); } return $query; } /** * Store an image of different types * * @param string $table * @param \Illuminate\Http\UploadedFile|\SplFileInfo|string $image * @return string * @throws \ErrorException If file copy fails. */ public static function storeImage(string $table, $image): string { /** @var \Illuminate\Http\UploadedFile $image */ if (\is_object($image) and get_class($image) === UploadedFile::class) { $newPath = self::prepareMoveFile($table, $image->getClientOriginalName()); $newPath = str_replace(storage_path('app'), '', $newPath); $image->storePubliclyAs( pathinfo($newPath, \PATHINFO_DIRNAME), pathinfo($newPath, \PATHINFO_BASENAME) ); return 'storage' . str_replace('/public', '', $newPath); } /** @var \SplFileInfo $image */ if (\is_object($image) and get_class($image) === \SplFileInfo::class) { $newPath = self::prepareMoveFile($table, $image->getBaseName()); if (!File::copy($image->getRealPath(), $newPath)) { throw new \ErrorException(sprintf( 'Failed to copy file %s to %s', $image->getRealPath(), $newPath )); } return \ltrim(str_replace(\storage_path('app/public'), '/storage', $newPath), '/'); } if (\is_string($image)) { return $image; } } /** * Prepare storage path and return * the image full path * * @param string $table Model table name. * @param string $filename The basename for the file. * @return string */ private static function prepareMoveFile(string $table, string $filename): string { $pathFolder = \storage_path(sprintf('app/public/images/%s', $table)); if (!File::exists($pathFolder)) { File::makeDirectory($pathFolder, 0755, true); } $filename = sprintf('%s-%s', Str::uuid(), $filename); $fullPath = sprintf('%s/%s', $pathFolder, $filename); if (File::exists($fullPath)) { File::delete($fullPath); } return $fullPath; } /** * Destroy uploaded image * * @param Model $model * @param string $attribute * @return void */ public function removeUploadedImage(Model $model, string $attribute) { if (File::exists(ltrim($model->{$attribute}, '/')) and \strpos($model->{$attribute}, '/storage') === 0) { File::delete(ltrim($model->{$attribute}, '/')); } } }