{$key} = $request->input($value); }; } /** * Fill schemaless */ public static function fillSchemalessField(string $schemalessAttribute = 'options'): Closure { return function ($request, $model, $attribute, $requestAttribute) use ($schemalessAttribute) { $model->{$schemalessAttribute}->set($attribute, $request->input($attribute)); }; } /** * Save by capitalizing firrt letter */ public static function capitalize(): Closure { return function ($request, $model, $attribute, $requestAttribute) { $model->{$attribute} = ucfirst($request->input($attribute)); }; } /** * Fill the slug field * * @param string|array $from * @param string $resource */ public static function fillSlug(string|array $from, ?string $resource): Closure { return function ($request, $model, $attribute, $requestAttribute) use ($from, $resource) { $slugSource = is_array($from) ? data_get($request, implode('.', $from)) : data_get($request, $from); $slug = Str::slug($slugSource); $query = $resource ? $resource::where('slug', $slug) : $model::where('slug', $slug); if ($query->exists()) { $slug .= Str::random(6); } $model->{$attribute} = $slug; }; } /** * Fill media file name */ public static function fillMediaFileName(): Closure { return fn ($originalFilename, $extension, $model) => sprintf('%s.%s', md5($originalFilename), $extension); } }