2017-02-05 6 views
3

私はlaravelのモデルでソフト削除機能を使用する方法を知っています。このような:laravelでカスタムソフト削除された列を使用する

class Flight extends Model 
{ 
    use SoftDeletes; 

    protected $dates = ['deleted_at']; 
} 

しかし、私はforceDeleterestorewithTrashedやなどの作品のような、関連するすべてのメソッドは、その列に基づいて、その機能のためにsender_deleted_atという名前のカスタム列を使用します。

私はthis Questionを書きましたが、正解を得ることができませんでした。

私はLaravel 5.3を使用しています。

答えて

7

SoftDeletesの形質が行を "削除" するために、このコードを使用しています。

protected function runSoftDelete() { 
     $query = $this->newQueryWithoutScopes()->where($this->getKeyName(), $this->getKey()); 
     $this->{$this->getDeletedAtColumn()} = $time = $this->freshTimestamp(); 
     $query->update([$this->getDeletedAtColumn() => $this->fromDateTime($time)]); 
} 

getDeletedAtColumn()のボディは次のとおりです。

public function getDeletedAtColumn() { 
    return defined('static::DELETED_AT') ? static::DELETED_AT : 'deleted_at'; 
} 

したがって、あなたがこれを行うことができます:

class Flight extends Model 
{ 
    use SoftDeletes;  
    protected $dates = ['my_deleted_at']; 
    const DELETED_AT = 'my_deleted_at'; 
} 
4

短い回答:モデルに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'; 
} 

説明:あなたは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'; 
} 

方法getDeletedAtColumnを見れば、あなたがその値を取得するよりも、あなたの実装クラスの定数名DELETED_ATを宣言した場合した場合はまあそれは実際にチェックしていますソフト削除列として単にdeleted_atを使用していません。

+0

感謝をあなたの返信のために、しかし、私は '静的:: DELETED_AT'が何であるか分からなかった。 'deleted_at'という言葉を私の希望する名前に変更すれば十分ですか? –

+0

これは、列namrを保持するクラスで定数を宣言しています – rummykhan

関連する問題