Another way of checking (a little bit more raw) is checking for the existence of method forceDelete
.
METHOD 1 - Check if forceDelete
method exists
if(method_exists($model, 'forceDelete')){
// Do your stuff here
}
Again, this is a little hack.
METHOD 2 - Using an interface
Generally is not optimal to check if a model uses a trait using insanceof
, technically speaking it would be more appropriate to create an interface UsesSoftDeletes
and make the model that actually use the SoftDeletes
trait implement it. Doing that you could simply make a check using instanceof
operator.
An example:
interface UsesSoftDeletes{
//
}
then in your model
class User extends Model implements UsesSoftDeletes
{
use SoftDeletes;
}
Then check with
if($model instanceof UsesSoftDeletes){
// Do your stuff here
}
EDIT - Checking for global scope
You can also check if the model uses the SoftDeletingScope
class (Laravel 5.x).
if($model->hasGlobalScope('Illuminate\Database\Eloquent\SoftDeletingScope')){
// Do your stuff
}