vendredi 17 septembre 2021

Laravel Soft Delete With custom column

i am trying to do softDelete with a custom column, I don't see my trait kicking in, for my case the active column will be used to indicate whether there is a soft delete or not, which be Bool.

<?php

namespace App\Traits;

use App\Scopes\MySoftDeletingScope;

/**
 * @author  
 * Trait MySoftDelete
 * @package App\Traits
 */
trait SoftDelete
{
    /**
     * Indicates if the model is currently force deleting.
     *
     * @var bool
     */
    protected $forceDeleting = false;

    /**
     * Boot the soft deleting trait for a model.
     *
     * @return void
     * @author  
     */
    public static function bootSoftDeletes()
    {
        static::addGlobalScope(new MySoftDeletingScope());
    }

    /**
     * Force a hard delete on a soft deleted model.
     *
     * @return bool|null
     * @author  
     */
    public function forceDelete()
    {
        $this->forceDeleting = true;

        $deleted = $this->delete();

        $this->forceDeleting = false;

        return $deleted;
    }

    /**
     * Perform the actual delete query on this model instance.
     *
     * @return mixed
     * @author  
     */
    protected function performDeleteOnModel()
    {
        if ($this->forceDeleting) {
            return $this->newQueryWithoutScopes()->where($this->getKeyName(), $this->getKey())->forceDelete();
        }

        return $this->runSoftDelete();
    }

    /**
     * Perform the actual delete query on this model instance.
     *
     * @return void
     * @author  
     */
    protected function runSoftDelete()
    {
        $query = $this->newQueryWithoutScopes()->where($this->getKeyName(), $this->getKey());

        $this->{$this->getActiveColumn()} = false;

        $query->update(['active' => false]);
    }

    /**
     * Restore a soft-deleted model instance.
     *
     * @return bool|null
     * @author  
     */
    public function restore()
    {
        // If the restoring event does not return false, we will proceed with this
        // restore operation. Otherwise, we bail out so the developer will stop
        // the restore totally. We will clear the deleted timestamp and save.
        if ($this->fireModelEvent('restoring') === false) {
            return false;
        }

        $this->{$this->getActiveColumn()} = true;

        // Once we have saved the model, we will fire the "restored" event so this
        // developer will do anything they need to after a restore operation is
        // totally finished. Then we will return the result of the save call.
        $this->exists = true;

        $result = $this->save();

        $this->fireModelEvent('restored', false);

        return $result;
    }

    /**
     * Determine if the model instance has been soft-deleted.
     *
     * @return bool
     * @author  
     */
    public function trashed()
    {
        return  !($this->{$this->getActiveColumn()});
    }

    /**
     * Register a restoring model event with the dispatcher.
     *
     * @param  \Closure|string  $callback
     * @return void
     * @author  
     */
    public static function restoring($callback)
    {
        static::registerModelEvent('restoring', $callback);
    }

    /**
     * Register a restored model event with the dispatcher.
     *
     * @param  \Closure|string  $callback
     * @return void
     * @author  
     */
    public static function restored($callback)
    {
        static::registerModelEvent('restored', $callback);
    }

    /**
     * Determine if the model is currently force deleting.
     *
     * @return bool
     * @author  
     */
    public function isForceDeleting()
    {
        return $this->forceDeleting;
    }

    /**
     * Get the name of the "deleted at" column.
     *
     * @return string
     * @author  
     */
    public function getActiveColumn()
    {
        return defined('static::ACTIVE') ? static::ACTIVE : 'active';
    }

    /**
     * Get the fully qualified "deleted at" column.
     *
     * @return string
     * @author  
     */
    public function getQualifiedActiveColumn()
    {
        return $this->getTable().'.'.$this->getActiveColumn();
    }
}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire