share('availableCountries', [ Country::france(), Country::belgium(), Country::luxembourg(), Country::guadeloupe(), Country::martinique(), Country::frenchGuiana(), Country::reunion(), Country::saintPierreAndMiquelon(), Country::mayotte(), Country::saintBarthelemy(), Country::saintMartinFrenchPart(), Country::wallisAndFutuna(), Country::frenchPolynesia(), Country::newCaledonia() ]); } /** * Undocumented function * * @param Builder $query The eloquent query builder * @param string $search The query string * @param string|array ...$field Either a simple array or a string * In case of a string the two fields would be concatenate to make the search * @return Builder */ protected function searchQuery( Builder $query, string $search, ...$field ): Builder { if (count($field)) { $table = $query->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) { 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 image to storage path * and put into model the image path * * @param Request $request * @param Model $model * @param string $attribute * @return void */ protected function storeUploadedImage(Request $request, Model &$model, string $attribute) { if ($request->{$attribute}) { // try delete old file if (File::exists(ltrim($model->{$attribute}, '/')) and \strpos($model->{$attribute}, '/storage') === 0) { File::delete(ltrim($model->{$attribute}, '/')); } /** @var \Illuminate\Http\UploadedFile $image */ $image = $request->{$attribute}; $newPath = $this->prepareMoveFile($image->getClientOriginalName(), $model->getTable()); $newPath = str_replace(storage_path('app'), '', $newPath); $image->storePubliclyAs(pathinfo($newPath, \PATHINFO_DIRNAME), pathinfo($newPath, \PATHINFO_BASENAME)); $model->{$attribute} = '/storage' . str_replace('/public', '', $newPath); } } protected function prepareMoveFile(string $filename, string $folder): string { $pathFolder = \storage_path("app/public/images/$folder"); 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; } }