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 AppTraits;

use AppScopesMySoftDeletingScope;

/**
* @author
* Trait MySoftDelete
* @package AppTraits
*/
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();
}
}

from Newest questions tagged laravel-5 – Stack Overflow https://ift.tt/3lxsIIw
via IFTTT

Related Posts

Codeigniter : Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE) in Laravel project

I’m getting following error: **Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE)** Note : It’s working in local but facing issue in production server. private…

Firebase receive notification while tab is active or on focus

What i want is to be able to perform an action when a user receives a notification while the browser is open and tab is active or…

Laravel’s alias loader does not find class

We have a legacy project that we cannot update and we need to make some changes in symfony’s Response.php in vendor. We have solved this by copying…

Laravel 5 – generic document management

I have a system where you can create different types of unique documents. For instance, one document is called Project Identified and this expects certain inputs. Originally,…

Laravel Nova limit the results in indexQuery

I ran intro a situation where I need to limit the results of a resource to only 3 results. To be more specific, based on the logged…

Auditoria en laravel 5.8 [closed]

Cómo puedo automatizar el registro de actividades de un usuario en laravel? Si un usuario ingresa a un app de laravel, debo guardar toda su actividas, a…

Leave a Reply

Your email address will not be published.