PLATFORM
  • Tails

    Create websites with TailwindCSS

  • Wave

    Start building the next great SAAS

  • Pines

    Alpine & Tailwind UI Library

  • Auth

    Plug'n Play Authentication for Laravel

  • Designer comingsoon

    Create website designs with AI

  • DevBlog comingsoon

    Blog platform for developers

  • Static

    Build a simple static website

  • SaaS Adventure

    21-day program to build a SAAS

Written By

Conditionally apply Laravel Soft Deleting scope

Conditionally apply Laravel Soft Deleting scope

Create an App state service class

<?php

namespace App\Services;

class AppState
{
    public bool $softDeletingScope = true;
}

Register the service in any service provider

use App\Services\AppState;
use Illuminate\Support\Facades\App;

public function register(): void
{
    $this->registerAppStateService();
}

public function boot(): void
{
    $this->controlSoftDeletingScope();
}

private function registerAppStateService(): void
{
    $this->app->singleton(AppState::class, fn (): AppState => new AppState());
}

private function controlSoftDeletingScope(): void
{
    App::macro('enableSoftDeletingScope', function (): void {
        app(AppState::class)->softDeletingScope = true;
    });

    App::macro('disableSoftDeletingScope', function (): void {
        app(AppState::class)->softDeletingScope = false;
    });

    App::macro('allowsSoftDeletingScope', function (): bool {
        return app(AppState::class)->softDeletingScope;
    });
}

Now you have access to these helpers anywhere in your code.

//Enable/disable the soft deleting scope

App::enableSoftDeletingScope()

App::disableSoftDeletingScope()

//Check if soft deleting scope is allowed or not.

App::allowsSoftDeletingScope() //returns true/false

//All helpers are available as not static versions as well
app()->enableSoftDeletingScope()
app()->disableSoftDeletingScope()
app()->allowsSoftDeletingScope()

Create a trait that conditionally applies the SoftDeletingScope. Use this trait instead of SoftDeletes on your Model.

<?php

namespace App\ModelTraits;

use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Facades\App;

trait conditionalSoftDeletes
{
    use SoftDeletes;

    public static function bootSoftDeletes(): void
    {
        if (App::allowsSoftDeletingScope()) {
            static::addGlobalScope(new SoftDeletingScope);
        }
    }
}

Example use case in a job

class FooJob implements ShouldQueue
{
    public function __construct(
        protected FooModel $model
    ) { }


    public function handle(): void
    {
        app()->disableSoftDeletingScope();

        //Now you could load missing relations, that includes soft deleted records
        $this->model->loadMissing( ... )

        app()->enableSoftDeletingScope(); // Don't forget to enable it again
    }

Important

Please observe that methods registered in the SoftDeletingScope class (withTrashed(), withoutTrashed(), onlyTrashed() and more) will not be available while the scope is disabled.

This article is related to this post: Laravel helper to detect if Laravel is running a job

Comments (0)

loading comments