我希望能够通过$user->delete();
方法软删除用户,但它似乎无法正常工作。无论我做什么,它总是难以删除。我已经关注了所有关于它的Laravel 4.2文档,我认为我已经正确配置了Sentry。这是我的代码:
<?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;
}
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!');
}
}
顺便说一下,我已经在
下发布了Sentry配置文件 /vendor/cartalyst/sentry/src/config/config.php
并将第123行更改为
'model' => 'User',
我还创建了迁移,以添加&#39; deleted_at&#39;我桌上的专栏,所以我不知所措。任何帮助将不胜感激。
提前致谢!
答案 0 :(得分:1)
所以,实际上解决方案非常简单。使用Sentry时,您没有使用预先配置了Laravel的Eloquent模型,而是使用Cartalyst自己的模型。所以我需要做的是在/vendor/cartalyst/sentry/src/Cartalyst/Sentry/Users/Eloquent/User.php
下的Sentry用户模型中获取软删除声明,而不是我打开此问题时尝试的声明。
这是我的Sentry用户模型:
<?php namespace Cartalyst\Sentry\Users\Eloquent;
use Illuminate\Database\Eloquent\SoftDeletingTrait;
use Illuminate\Database\Eloquent\Model;
use Cartalyst\Sentry\Groups\GroupInterface;
use Cartalyst\Sentry\Hashing\HasherInterface;
use Cartalyst\Sentry\Users\LoginRequiredException;
use Cartalyst\Sentry\Users\PasswordRequiredException;
use Cartalyst\Sentry\Users\UserAlreadyActivatedException;
use Cartalyst\Sentry\Users\UserExistsException;
use Cartalyst\Sentry\Users\UserInterface;
class User extends Model implements UserInterface {
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'users';
现在$user->delete();
正常工作,添加'deleted_at'时间戳而不是销毁用户。
但是有些问题。我还没有时间测试所有内容,但我遇到了一些困难,例如Sentry模型无法查询已删除的结果,因此无法检索软删除的用户。现在我只使用User::withTrashed();
Laravel方法做到这一点,效果很好。
不要忘记在/app/models/User.php
下扩展Cartalyst的模型!
希望这也适用于其他人!
答案 1 :(得分:1)
此设置对我有用:
在用户模型上:app / models / User.php
<?php
use Cartalyst\Sentry\Users\Eloquent\User as SentryUserModel;
use Illuminate\Database\Eloquent\SoftDeletingTrait;
class User extends SentryUserModel {
use SoftDeletingTrait;
protected $softDelete = true;
protected $dates = ['deleted_at'];
在我的用户迁移中添加了deleted_at字段:
class UpdateUsersTable extends Migration {
public function up()
{
Schema::table('users', function(Blueprint $table)
$table->datetime('deleted_at');
});
}
public function down()
{
Schema::table('users', function(Blueprint $table)
{
$table->dropColumn('deleted_at');
});
}
发布了哨兵配置文件
php artisan config:publish --path="vendor/cartalyst/sentry/src/config/config.php" cartalyst/sentry
我在该已发布文件上更改了第123行以使用laravel用户模型:(app / config / packages / cartalyst / sentry / config.php)
// 'model' => 'Cartalyst\Sentry\Users\Eloquent\User',
'model' => 'User',
它有效甚至可行:User :: withTrashed() - &gt; get();
如果这不起作用,请尝试重新清除数据库运行迁移。
这种方法的好处在于,即使您更新了哨兵,也不会丢失供应商文件夹中的更改,您的应用程序也不会有错误。祝你好运!