【发布时间】:2015-05-12 13:31:14
【问题描述】:
我希望能够通过$user->delete(); 方法软删除用户,但它似乎无法正常工作。无论我做什么,它总是硬删除。我已经关注了所有关于它的 Laravel 4.2 文档,并且我认为我已经正确配置了 Sentry。这是我的代码:
用户.php
<?php
use Cartalyst\Sentry\Sentry;
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
use Illuminate\Database\Eloquent\SoftDeletingTrait;
class User extends Cartalyst\Sentry\Users\Eloquent\User implements UserInterface, RemindableInterface
{
protected $fillabe = array('username','email','password','password_temp','code','active');
use UserTrait, RemindableTrait, SoftDeletingTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
//Soft deleting
protected $dates = ['deleted_at'];
public $timestamps = true;
}
Controller.php
public function postDeactivate()
{
$validator = Validator::make(Input::all(), array('password' => 'required|min:6'));
if ($validator->fails()) {
return Redirect::route('account-deactivate')->withErrors($validator);
} else {
try {
$user = Sentry::getUser();
if ($user->checkPassword(Input::get('password'))) {
Mail::send(
'emails.deactivate',
array('username' => $user->username),
function($message) use ($user) {
$message->to($user->email, $user->username)->subject('Hope to see you soon!');
}
);
if ($user->delete()) {
return Redirect::route('home') ->with('global','Account deactivated!');
} else {
return Redirect::route('account-deactivate')->with('global','Error!');
}
} else {
return Redirect::route('account-deactivate')->with('global','Wrong password.');
}
} catch (Cartalyst\Sentry\Users\UserNotFoundException $e) {
return Redirect::route('account-sign-in-get')->with('message','User not found!');
}
}
顺便说一下,我已经在
下发布了哨兵配置文件/vendor/cartalyst/sentry/src/config/config.php
并将第 123 行更改为
'model' => 'User',
我还创建了迁移以在我的表上添加“deleted_at”列,所以我很茫然。任何帮助将不胜感激。
提前致谢!
【问题讨论】:
-
嗨,Gabriel,您找到解决方案了吗?我面临着同样的问题。谢谢!
-
我做到了!我会尽快发布答案:)
标签: php laravel soft-delete cartalyst-sentry