W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
當(dāng)模型 belongsTo 另一個模型時,比方說一個 Comment 屬于一個 Post ,如果能在子模型被更新時,更新上層的時間戳,這將會很有用。例如,當(dāng) Comment 模型更新時,您可能想要能夠同時自動更新 Post 的 updated_at 時間戳。 Eloquent 讓事情變得很簡單。只要在子關(guān)聯(lián)的類里,把關(guān)聯(lián)方法名稱加入 touches 屬性即可:
class Comment extends Model {
protected $touches = ['post'];
public function post()
{
return $this->belongsTo('App\Post');
}
}
現(xiàn)在,當(dāng)您更新 Comment 時,對應(yīng)的 Post 會自動更新 updated_at 字段:
$comment = Comment::find(1);
$comment->text = 'Edit to this comment!';
$comment->save();
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: