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µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡