问题
How do I determine if a model uses soft deletes in Laravel 4.2?
In the Laravel API I found the function isSoftDeleting(), but apparently that was removed from Laravel 4.2 now that it uses the SoftDeletingTrait.
How would I go about determining if a model uses soft deletes now?
回答1:
If you want to check programatically whether a Model uses soft deletes you can use the PHP function class_uses
to determine if your model uses the SoftDeletingTrait
// You can use a string of the class name
$traits = class_uses('Model');
// Or you can pass an instance
$traits = class_uses($instanceOfModel);
if (in_array('SoftDeletingTrait', $traits))
{
// Model uses soft deletes
}
// You could inline this a bit
if (in_array('SoftDeletingTrait', class_uses('Model')))
{
// Model uses soft deletes
}
回答2:
I needed to detect soft deletion on a model where the trait had been included in a parent class, so class_uses()
did not work for me. Instead, I checked for the bootSoftDeletingTrait()
method. Something along the lines of:
// Class Name
$usesSoftDeletes = method_exists('User', 'bootSoftDeletingTrait');
or
// Model Instance
$usesSoftDeletes = method_exists($model, 'bootSoftDeletingTrait');
should work.
回答3:
There's basically not a direct approach on knowing if a model soft deletes by calling a function since the method isSoftDeleting()
has been removed from 4.2. You know if a model is using soft delete if the use SoftDeletingTrait;
is present in your Model's class and when the deleted_at
column exists in your database table.
You can basically trust Laravel on removing a record from your database using soft delete when you have defined the use SoftDeletingTrait
in your model's class, and have the deleted_at
(is a TIMESTAMP) in your model's database table.
回答4:
Well, I figured out out a good enough solution for my needs.
First I make this call:
$traits = class_uses($model);
Then I check for the the softdeletingtrait
$usesSoftDeletes = in_array('Illuminate\Database\Eloquent\SoftDeletingTrait', $traits);
At least this way I avoid having to call the database for each model I test. Though it'll break if they change the SoftDeletingTrait name or location later on...
回答5:
For Laravel 5.x only
If checking on main model:
//class_uses retrieves a list of traits from the object passed into an array
//Hence in_array check for name of trait (in string format)
//@param bool $usesSoftDeletes
$usesSoftDeletes = in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($model));
If you are checking on the main model's relationship, use the following:
//Replace `myRelationshipName` by the name of the relationship you are checking on.
//getRelated() function fetches the class of the relationship.
//@param bool $relatedUsesSoftDeletes
$relatedModel = $model->myRelationshipName()->getRelated();
$relatedUsesSoftDeletes = in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($relatedModel));
来源:https://stackoverflow.com/questions/25140768/how-to-determine-if-a-model-uses-soft-deletes-in-laravel-4-2