laravel ÌáÐÑûÓÐÖ÷¼ü
ÔÚʹÓà laravel ¾ÙÐÐÊý¾Ý¿â²Ù×÷ʱ£¬ÓÐʱ¼ä»áÓöµ½Ò»¸ö³£¼ûµÄÎÊÌ⣺ûÓÐÖ÷¼ü¡£ÕâÊÇÓÉÓÚÔÚ laravel ĬÈϵÄÄ£×ÓÖУ¬»á×Ô¶¯¼Ù¶¨Ö÷¼üΪ id£¬ÈôÊÇÊý¾Ý¿âÖÐûÓÐ id ×ֶλòÕ߸Ã×Ö¶ÎûÓб»ÉèÖÃΪÖ÷¼ü£¬¾Í»áÌáÐÑûÓÐÖ÷¼üµÄ¹ýʧ¡£
ÄÇô£¬ÔõÑù½â¾öûÓÐÖ÷¼üµÄÎÊÌâÄØ£¿ÏÂÃ潫»®·Ö´ÓÊý¾Ý¿âºÍ Laravel Ä£×ӵĽǶÈÀ´ÏÈÈݽâ¾öÒªÁì¡£
Ò»¡¢Êý¾Ý¿â½â¾öÒªÁì
ÉèÖÃÖ÷¼ü
ÔÚ MySQL Êý¾Ý¿âÖУ¬¿ÉÒÔʹÓà ALTER TABLE Óï¾äÀ´Ð޸ıíµÄ½á¹¹£¬°üÀ¨ÉèÖÃÖ÷¼ü¡£Àý£º
ALTER TABLE ±íÃû ADD PRIMARY KEY(×Ö¶ÎÃû);
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬±íÃûºÍ×Ö¶ÎÃûÐèÒª×ÔÐÐÌæ»»¡£
½¨ÉèеÄÖ÷¼ü
ÈôÊÇÊý¾Ý¿âÖÐûÓÐÊʺϵÄÖ÷¼ü×ֶΣ¬¿ÉÒÔͨ¹ýн¨Ò»¸öÖ÷¼ü×Ö¶ÎÀ´½â¾öÎÊÌâ¡£Àý£º
ALTER TABLE ±íÃû ADD ×Ö¶ÎÃû INT AUTO_INCREMENT PRIMARY KEY;
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬±íÃûºÍ×Ö¶ÎÃûÐèÒª×ÔÐÐÌæ»»¡£AUTO_INCREMENT ÌåÏÖ¸Ã×Ö¶ÎÊÇ×ÔÔöµÄ¡£
¶þ¡¢Laravel Ä£×Ó½â¾öÒªÁì
×Ô½ç˵Ö÷¼ü
ÔÚ Laravel Ä£×ÓÖУ¬¿ÉÒÔͨ¹ý $primaryKey ÊôÐÔÀ´×Ô½ç˵Ö÷¼ü¡£Àý£º
class User extends Model { protected $primaryKey = 'user_id'; }
µÇ¼ºó¸´ÖÆ
ÕâÀォÖ÷¼üÉèÖÃΪ user_id¡£
½ûÓÃ×ÔÔöÖ÷¼ü
ÈôÊÇʹÓõÄÊÇ·Ç×ÔÔöÖ÷¼ü£¬ÐèÒª½« $incrementing ÊôÐÔÉèÖÃΪ false£¬ÒÔ½ûÓÃ×ÔÔöÖ÷¼ü¡£Àý£º
class Product extends Model { public $incrementing = false; }
µÇ¼ºó¸´ÖÆ
ÊÖ¶¯Ö¸¶¨Ö÷¼üÖµ
ÔÚһЩÌض¨µÄ³¡¾°Ï£¬¿ÉÄÜÐèÒªÊÖ¶¯Ö¸¶¨Ö÷¼üÖµ¡£Õâ¿ÉÒÔͨ¹ýÔÚÄ£×ÓÖÐÉèÖà $keyType ºÍ $fillable ÊôÐÔÀ´ÊµÏÖ¡£Àý£º
class Order extends Model { protected $keyType = 'string'; protected $fillable = ['order_id', 'product_name']; }
µÇ¼ºó¸´ÖÆ
ÕâÀォÖ÷¼üÀàÐÍÉèÖÃΪ×Ö·û´®£¬²¢½ç˵ÁË¿ÉÌî³äµÄ×ֶΡ£
×ܽá
ͨ¹ýÒÔÉÏÒªÁ죬¿ÉÒÔ½â¾ö Laravel Ä£×ÓÖÐûÓÐÖ÷¼üµÄÎÊÌâ¡£ÔÚÏÖʵ¿ª·¢ÖУ¬¿ÉÒÔƾ֤ÏêϸÐèÇóÑ¡ÔñÏìÓ¦µÄÒªÁì¾ÙÐнâ¾ö¡£Í¬Ê±£¬ÐèҪעÖØ°ü¹ÜÖ÷¼üµÄΨһÐÔ£¬×èÖ¹¿ÉÄÜ·ºÆðµÄ³åÍ»ÎÊÌâ¡£
ÒÔÉϾÍÊÇlaravel ÌáÐÑûÓÐÖ÷¼üµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡