Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Filament Restore Or Create Laravel Package

martinpetricko/filament-restore-or-create

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install the Package

    composer require martinpetricko/filament-restore-or-create
    

    Publish the config (if needed):

    php artisan vendor:publish --provider="MartinPetricko\FilamentRestoreOrCreate\FilamentRestoreOrCreateServiceProvider"
    
  2. Register the Plugin Add to your app/Providers/Filament/AdminPanelProvider.php:

    public function panel(Panel $panel): Panel
    {
        return $panel
            ->plugins([
                \MartinPetricko\FilamentRestoreOrCreate\FilamentRestoreOrCreatePlugin::make(),
            ]);
    }
    
  3. First Use Case Apply to a Filament Resource by adding the trait to your model:

    use MartinPetricko\FilamentRestoreOrCreate\Traits\RestoreOrCreate;
    
    class Post extends Model
    {
        use RestoreOrCreate;
    }
    

    Ensure your model uses soft deletes (SoftDeletes trait) and has a deleted_at column.


Implementation Patterns

Core Workflow

  1. Form Submission When a user submits a create form, the package intercepts the request and checks for soft-deleted records matching the input data.

  2. Detection Logic Override getSimilarRecords() in your model to define matching criteria:

    public function getSimilarRecords(array $attributes): Collection
    {
        return static::where('title', $attributes['title'])
            ->whereNull('deleted_at')
            ->get();
    }
    

    Default behavior uses where() on all fillable fields.

  3. Modal Presentation If matches are found, a modal displays:

    • Deleted record details (customizable via getRestoreModalContent()).
    • "Restore" button (triggers restore() on the model).
  4. Fallback to Create If no matches or user declines, the original create logic proceeds.

Integration Tips

  • Customize Modal Content Override getRestoreModalContent() in your model:
    public function getRestoreModalContent(SoftDeletingModel $record): string
    {
        return view('custom.restore-modal', ['record' => $record]);
    }
    
  • Conditional Activation Disable for specific forms via config:
    'enabled_for' => [
        'App\Models\Post', // Only enable for Post model
    ],
    
  • Bulk Actions Extend to work with bulk imports by hooking into registerCreateActions() in your Resource.

Gotchas and Tips

Pitfalls

  1. Soft Deletes Required The package only works with soft-deleted models. Ensure your model uses SoftDeletes and has deleted_at column.

  2. Case-Sensitive Matching Default getSimilarRecords() uses exact matches. For case-insensitive searches:

    return static::whereRaw("LOWER(title) = LOWER('{$attributes['title']}')")
        ->whereNull('deleted_at')
        ->get();
    
  3. Performance on Large Tables Avoid complex queries in getSimilarRecords(). Add indexes to frequently queried columns:

    php artisan schema:dump
    

    Then manually add indexes in migrations.

  4. Caching Conflicts If using Filament’s caching, clear it after restoring a record to avoid stale data:

    public function restore()
    {
       $this->forceFill(['deleted_at' => null])->save();
       Cache::forget('filament-resource-cache-key');
    

}


### Debugging
- **Log Detection Queries**
Enable debug mode in config:
```php
'debug' => env('FILAMENT_RESTORE_OR_CREATE_DEBUG', false),

Check logs for SQL queries during detection.

  • Modal Not Showing? Verify:
    • The model uses RestoreOrCreate trait.
    • The form is submitting to a Filament Resource (not a standalone Blade form).
    • No JavaScript errors (check browser console).

Extension Points

  1. Custom Matching Logic Extend the RestoreOrCreate trait to add fuzzy matching (e.g., Levenshtein distance):

    use Similarity\Similarity;
    
    public function getSimilarRecords(array $attributes): Collection
    {
        return static::where('title', 'like', "%{$attributes['title']}%")
            ->whereNull('deleted_at')
            ->where(function ($query) use ($attributes) {
                $query->orWhere('slug', Similarity::levenshtein($attributes['slug']), '<', 3);
            })
            ->get();
    }
    
  2. Post-Restore Redirects Override getRestoreRedirectUrl() in your model:

    public function getRestoreRedirectUrl(SoftDeletingModel $record): string
    {
        return route('filament.resources.posts.edit', $record);
    }
    
  3. Disable for Specific Fields Exclude fields from matching in config:

    'ignore_fields' => ['created_at', 'updated_at'],
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours