我的模型测试如下
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Test extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
public function __construct() {
if (!\App::environment('production')) {
$this->table='test_stage';
}
}
Run Code Online (Sandbox Code Playgroud)
我确保test_stage表中有一个'deleted_at'列.但软删除不起作用.使用delete()方法永久删除表中的记录.作为验证的附加步骤,我手动为某些列添加了"deleted_at"值.但查询模型仍然给我软删除记录.
此外,完全删除模型构造函数,并使用以下命令定义表名:
protected $table = 'test_stage';
Run Code Online (Sandbox Code Playgroud)
奇迹般有效!那是软删除神奇地再次开始工作.
或者有没有办法根据环境定义表名而无需定义构造函数?