×ðÁú¿­Ê±¹ÙÍøµÇ¼

laravelÔõô×÷·Ïcreated_at

ÔÚlaravel¿ò¼ÜÖУ¬Ã¿¸öeloquentÄ£×Ó¶¼Ä¬ÈÏ°üÀ¨ÁËcreated_atºÍupdated_atÁ½¸öʱ¼ä´Á×ֶΡ£ÕâÁ½¸ö×ֶμͼÁËÄ£×ӵĽ¨ÉèºÍ¸üÐÂʱ¼ä¡£ÕâÑù×öµÄÀûÒæÊÇÀû±ã¼Í¼Êý¾ÝµÄ¸üÐÂʱ¼ä£¬ÒÔ¼°Àû±ãʵÏÖһЩ¹¦Ð§£¬ºÃ±Èƾ֤½¨Éèʱ¼äºÍ¸üÐÂʱ¼äÅÅÐòµÈµÈ¡£

È»¶ø£¬ÔÚijЩ³¡¾°Ï£¬ÎÒÃÇ¿ÉÄܲ»ÐèÒªÕâÁ½¸öʱ¼ä´Á×ֶΡ£ºÃ±ÈÓÐЩÀϵÄÊý¾Ý¿â±í¿ÉÄÜûÓÐÕâÁ½¸ö×ֶΣ¬»òÕßÎÒÃÇ»ù´¡²»ÐèÒª¼Í¼Êý¾ÝµÄ½¨ÉèºÍ¸üÐÂʱ¼ä¡£ÔÚÕâÖÖÇéÐÎÏ£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÔÚÄ£×ÓÖмÓÈëÏÂÃæÕâÁ½ÐдúÂëÀ´×÷·Ï created_at ºÍ updated_at£º

public $timestamps = false;

µÇ¼ºó¸´ÖÆ

ÕâÑù×ö¿ÉÒÔÔÚÄ£×ÓÖÐ×÷·Ï created_at ºÍ updated_at ×ֶΡ£¿ÉÊÇҪעÖصÄÊÇ£¬×÷·ÏÕâÁ½¸öʱ¼ä´Á×ֶλᵼÖÂһЩLaravelÄÚÖõĹ¦Ð§Ê§Ð§¡£ºÃ±ÈÄ£×ÓÎÞ·¨Í¨¹ý created_at ºÍ updated_at ×ֶξÙÐÐÅÅÐò£¬Ò²ÎÞ·¨×Ô¶¯¼Í¼Êý¾ÝµÄ½¨ÉèºÍ¸üÐÂʱ¼ä¡£

ÈôÊÇÎÒÃÇÖ»ÐèÒª×÷·ÏÆäÖÐÒ»¸öʱ¼ä´Á×ֶΣ¬ºÃ±ÈÖ»×÷·Ï created_at£¬ÄÇô¿ÉÒÔÕâÑù×ö£º

// Ö»×÷·Ï created_at
const UPDATED_AT = 'updated_at';

µÇ¼ºó¸´ÖÆ

Õâ¸öÒªÁìÊǽ« UPDATED_AT ³£Á¿¸³ÖµÎª updated_at ×Ö·û´®£¬¶øĬÈÏÇéÐÎÏ UPDATED_AT µÄÖµÊÇ updated_at£¬Òò´ËÕâÑù×öÖ»×÷·ÏÁË created_at¡£

µ±ÎÒÃÇ×÷·ÏÁËÕâЩʱ¼ä´Á×ֶκó£¬ÈôÊÇÎÒÃÇÐèҪʵÏÖ×Ô¼ºµÄÊý¾Ý½¨Éèʱ¼äºÍ¸üÐÂʱ¼äµÄ¼Í¼£¬ÄÇôÎÒÃǾÍÐèÒªÊÖ¶¯±àд´úÂëÀ´ÊµÏÖ¡£ÏÂÃæÊÇÒ»¸öÀý×Ó£º

// ÔÚÄ£×ÓÖÐʵÏÖ×Ô½ç˵ʱ¼ä´Á×Ö¶Î
protected $dateFormat = 'U';
protected static function boot()
{
    parent::boot();
    static::creating(function ($model) {
        $model->create_time = $model->freshTimestamp();
    });
    static::updating(function ($model) {
        $model->update_time = $model->freshTimestamp();
    });
}

µÇ¼ºó¸´ÖÆ

Õâ¸öÀý×ÓÖУ¬ÎÒÃÇÔÚÄ£×ÓÖнç˵ÁË create_time ºÍ update_time Á½¸ö×Ö¶ÎÀ´¼Í¼Êý¾ÝµÄ½¨Éèʱ¼äºÍ¸üÐÂʱ¼ä¡£Í¬Ê±£¬ÎÒÃÇͨ¹ý½ç˵ $dateFormat ÊôÐÔÀ´ÐÞ¸ÄĬÈϵÄʱ¼äÃûÌá£ÔÚ boot ÒªÁìÖУ¬ÎÒÃÇͨ¹ý creating ºÍ updating ÊÂÎñÀ´Îª create_time ºÍ update_time ¸³Öµ¡£freshTimestamp ÒªÁì¿ÉÒÔ»ñÈ¡×îеÄʱ¼ä´Á¡£

×ܽáһϣ¬×÷·Ï created_at ºÍ updated_at ¿ÉÒÔͨ¹ýÔÚÄ£×ÓÖÐÌí¼Ó $timestamps = false »òÐÞ¸Ä UPDATED_AT ³£Á¿À´ÊµÏÖ¡£×÷·ÏÕâЩʱ¼ä´Á×ֶλáÓ°ÏìLaravelÖÐһЩÄÚÖõĹ¦Ð§£¬ºÃ±ÈÅÅÐòºÍ×Ô¶¯¼Í¼ʱ¼äµÈµÈ¡£ÈôÊÇÐèÒª×Ô¼ºÊµÏÖʱ¼ä´ÁµÄ¼Í¼£¬¿ÉÒÔͨ¹ýÊÖ¶¯±àд´úÂëÀ´ÊµÏÖ¡£

ÒÔÉϾÍÊÇlaravelÔõô×÷·Ïcreated_atµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ