A.B*_*per 7 php laravel laravel-5.3
我知道如何在laravel模型中使用Soft Deleting功能.像这样 :
class Flight extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
}
Run Code Online (Sandbox Code Playgroud)
但我想用命名的自定义柱sender_deleted_at为特征,其中像所有的相关方法forceDelete,restore,withTrashed和等工作,基于该列.
我写了这个问题但是我找不到正确的答案.
我正在使用Laravel 5.3.
apo*_*fos 19
SoftDeletes trait使用此代码"删除"一行:
protected function runSoftDelete() {
$query = $this->newQueryWithoutScopes()->where($this->getKeyName(), $this->getKey());
$this->{$this->getDeletedAtColumn()} = $time = $this->freshTimestamp();
$query->update([$this->getDeletedAtColumn() => $this->fromDateTime($time)]);
}
Run Code Online (Sandbox Code Playgroud)
身体getDeletedAtColumn()是:
public function getDeletedAtColumn() {
return defined('static::DELETED_AT') ? static::DELETED_AT : 'deleted_at';
}
Run Code Online (Sandbox Code Playgroud)
因此你可以这样做:
class Flight extends Model
{
use SoftDeletes;
protected $dates = ['my_deleted_at'];
const DELETED_AT = 'my_deleted_at';
}
Run Code Online (Sandbox Code Playgroud)
简答:只需const DELETED_AT在模型中声明一个并给它一个你想要使用的列名.
<?php
namespace App;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use SoftDeletes;
use Notifiable;
const DELETED_AT = 'deletedAt';
}
Run Code Online (Sandbox Code Playgroud)
说明:如果你看一下方法好getDeletedAtColumn于trait Illuminate\Database\Eloquent\SoftDeletes
/**
* Get the name of the "deleted at" column.
*
* @return string
*/
public function getDeletedAtColumn()
{
return defined('static::DELETED_AT') ? static::DELETED_AT : 'deleted_at';
}
Run Code Online (Sandbox Code Playgroud)
它实际上检查你是否DELETED_AT在实现类中声明了一个常量名,而不是获取该值,如果你还没有简单地deleted_at用作软删除列.