laravelµÄÄ£×Ó²ãÔÚÄÄ
laravelµÄÄ£×Ó²ãÔÚÄÄ
LaravelÊÇÒ»¸öÊ¢ÐеÄPHP WebÓ¦ÓóÌÐò¿ò¼Ü£¬ÆÕ±éÓÃÓÚ¹¹½¨¸ßЧ¡¢¿ÉÀ©Õ¹ºÍÒ×ÓÚά»¤µÄWebÓ¦ÓóÌÐò¡£ÆäÖÐÒ»¸öÒªº¦×é³É²¿·ÖÊÇÄ£×Ӳ㣬ÕâÊÇÓÃÓÚÖÎÀíÓ¦ÓóÌÐòÊý¾ÝµÄ½¹µã×é¼þ¡£ÔÚLaravelÖУ¬Ä£×Ó²ãÓÐ׿«ÆäÖ÷ÒªµÄ×÷Ó㬱¾ÎĽ«ÏÈÈÝlaravelµÄÄ£×Ó²ãÔÚÄÄ£¬²¢¶ÔÆä¾ÙÐÐÏêϸÆÊÎö¡£
Ò»¡¢LaravelµÄÄ£×Ó²ã¸ÅÊö
ÔÚMVC£¨Model-View-Controller£©¿ò¼ÜÖУ¬Ä£×Ӳ㣨Model£©ÊÇÓÃÓÚ´¦ÀíÊý¾ÝºÍÊý¾Ý¿â½»»¥µÄ½¹µã×é¼þ¡£ÔÚLaravelÖУ¬Ä£×Ó²ã¼ÌÐøÁËEloquent ORM£¨Object-Relational Mapping£©ÊµÏÖ£¬¼ò»¯ÁËÊý¾ÝµÄ½»»¥£¬²¢ÌṩÁ˱ã½ÝµÄÒªÁì¾ÙÐÐÊý¾Ý¿â²Ù×÷¡£
¶þ¡¢LaravelµÄÄ£×Ó²ãλÖÃ
ÔÚLaravelÖУ¬ËùÓеÄÄ£×Ó¹¤¾ß¶¼ÔÚappĿ¼ÏµÄModels×ÓĿ¼ÖС£ÈôÊÇÓ¦ÓÃʹÓÃÁËLaravelµÄĬÈÏĿ¼½á¹¹£¬ÄÇôӦ¸Ã¿ÉÒÔÔÚapp/ModelsĿ¼ÖÐÕÒµ½ÕâЩģ×ÓÎļþ¡£
¾Ù¸öÀý×Ó£¬ÈôÊÇÎÒÃÇÓÐÒ»¸öÃûΪUserµÄÄ£×Ó£¬ÄÇôËüÓ¦¸Ã¶ÔÓ¦app/Models/User.phpÕâ¸öÎļþ¡£ÔÚÕâ¸öÎļþÖУ¬ÎÒÃÇ¿ÉÒÔ½ç˵ÓëÖ®¶ÔÓ¦µÄÊý¾Ý±í¡¢¹ØÁª¹ØϵºÍÊý¾Ý²Ù×÷µÈÄÚÈÝ¡£
Èý¡¢LaravelÄ£×Ó²ãµÄ¹¦Ð§
LaravelµÄÄ£×Ó²ãÌṩÁËһϵÁÐÇ¿Ê¢µÄ¹¦Ð§£¬°üÀ¨ÒÔϼ¸¸ö·½Ãæ¡£
Êý¾Ý±íÓ³Éä
ÔÚLaravelÖУ¬Ä£×ÓÀàͨ³£ÓëÊý¾Ý¿â±íÓ³É䣬Ҳ¾ÍÊÇ˵£¬Ä£×ÓÀàµÄ¹¤¾ß¾Í´ú±í×ÅÒ»ÌõÊý¾Ý¿â±íÖеÄÊý¾Ý¡£ÔÚÄ£×ÓÀàÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃ$tableÊôÐÔÖ¸¶¨±íÃû£¬ÕâÑùLaravel¾Í»á×Ô¶¯½«Ä£×Ó¹¤¾ßÓ³Éäµ½¸Ã±íÉÏ¡£
ÀýÈ磬ÈôÊÇÎÒÃÇÓÐÒ»¸öÃûΪUserµÄÄ£×Ó£¬¿ÉÒÔÕâÑùÖ¸¶¨ËüÒªÓ³ÉäµÄÊý¾Ý±í£º
class User extends Model { /** * ¸ÃÄ£×Ó¹ØÁªµÄÊý¾Ý¿â±í * * @var string */ protected $table = 'users'; }
µÇ¼ºó¸´ÖÆ
½ç˵¹ØÁª¹Øϵ
Ä£×Ó²ãÖеĹØÁª¹ØϵָµÄÊÇÄ£×Ó¹¤¾ßÖ®¼äµÄ¹ØÁª¹Øϵ£¬°üÀ¨Ò»¶ÔÒ»¡¢Ò»¶Ô¶à¡¢¶à¶Ô¶àµÈÀàÐÍ¡£ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔʹÓùٷ½ÌṩµÄ¹ØÁª¹ØϵҪÁìÀ´½çËÃ÷ÈÕâЩ¹Øϵ¡£
ÀýÈ磬ÈôÊÇÎÒÃÇÓÐÒ»¸öOrderºÍOrderItemÁ½¸öÄ£×Ó£¬ÆäÖÐÒ»¸ö¶©µ¥¿ÉÒÔ°üÀ¨¶à¸ö¶©µ¥ÏÄÇôÎÒÃÇ¿ÉÒÔÕâÑù½ç˵ËüÃÇÖ®¼äµÄ¹ØÁª¹Øϵ£º
class Order extends Model { /** * ¶©µ¥Ëù°üÀ¨µÄ¶©µ¥Ïî */ public function orderItems() { return $this->hasMany(OrderItem::class); } }
µÇ¼ºó¸´ÖÆ
class OrderItem extends Model { /** * ËùÊô¶©µ¥ */ public function order() { return $this->belongsTo(Order::class); } }
µÇ¼ºó¸´ÖÆ
ʹÓÃÄ£×Ó¾ÙÐÐÊý¾Ý²Ù×÷
ÔÚÄ£×Ó²ãÖУ¬ÎÒÃÇÒ²¿ÉÒÔʹÓÃÄ£×Ó¹¤¾ß¾ÙÐÐÊý¾Ý²Ù×÷£¬ÈçÌí¼Ó¡¢ÅÌÎÊ¡¢É¾³ý»ò¸üÐÂÊý¾ÝµÈ¡£ÔÚLaravelÖУ¬ÕâЩ²Ù×÷¶¼°üÀ¨ÔÚEloquent ORMÖС£
ÀýÈ磬ÈôÊÇÎÒÃÇÏë»ñȡij¸ö¶©µ¥µÄËùÓж©µ¥Ï¿ÉÒÔʹÓÃModel::find()ÒªÁìÀ´»ñÈ¡¸Ã¶©µ¥¹¤¾ß£¬È»ºóŲÓÃorderItems()ÒªÁì»ñÈ¡ËüµÄËùÓж©µ¥Ï
$order = Order::find(1); $orderItems = $order->orderItems();
µÇ¼ºó¸´ÖÆ
×Ô½ç˵»á¼ûÆ÷ºÍÐÞ¸ÄÆ÷
LaravelµÄÄ£×Ӳ㻹ÔÊÐíÎÒÃÇ×Ô½ç˵»á¼ûÆ÷ºÍÐÞ¸ÄÆ÷£¬ÒÔ±ãÔ½·¢ÎÞаµØ²Ù×÷Êý¾Ý¡£»á¼ûÆ÷ÓÃÓÚÔÚ´ÓÄ£×ÓÖлñȡij¸öÊôÐÔʱ¶ÔÆä¾ÙÐÐÃûÌû¯´¦Àí£¬ÐÞ¸ÄÆ÷Ôò¿ÉÒÔÔÚÉèÖÃij¸öÊôÐÔʱ¶ÔÆä¾ÙÐвÙ×÷¡£
ÀýÈ磬ÎÒÃÇ¿ÉÒÔÔÚUserÄ£×ÓÖнç˵һ¸öaccessorÓÃÓÚ»ñÈ¡Óû§µÄÈ«Ãû£º
class User extends Model { /** * »á¼ûÆ÷£¬»ñÈ¡Óû§µÄÈ«Ãû * * @return string */ public function getFullNameAttribute() { return $this->first_name . ' ' . $this->last_name; } }
µÇ¼ºó¸´ÖÆ
ÕâÑù£¬ÎÒÃǾͿÉÒÔÕâÑù»ñÈ¡Óû§¹¤¾ßµÄÈ«Ãû£º
$user = User::find(1); $fullName = $user->full_name; // 'John Doe'
µÇ¼ºó¸´ÖÆ
ËäÈ»£¬ÐÞ¸ÄÆ÷Ò²ÊÇÒ»ÑùµÄ£¬Ö»ÐèÒªÓÃset¿ªÍ·ÃüÃûÒªÁì¼´¿É¡£
ËÄ¡¢×ܽá
LaravelµÄÄ£×Ó²ãÊÇ´¦ÀíÊý¾ÝºÍÊý¾Ý¿â½»»¥µÄ½¹µã×é¼þ£¬ÓÐ׿«ÆäÖ÷ÒªµÄ×÷Óá£ÔÚLaravelÖУ¬ËùÓеÄÄ£×Ó¹¤¾ß¶¼ÔÚappĿ¼ÏµÄModels×ÓĿ¼ÖУ¬ÎÒÃÇ¿ÉÒÔÔÚÄ£×ÓÀàÖнç˵ÓëÖ®¶ÔÓ¦µÄÊý¾Ý±í¡¢¹ØÁª¹ØϵºÍÊý¾Ý²Ù×÷µÈÄÚÈÝ¡£ÕâЩģ×ÓÀàÌṩÁ˸»ºñµÄ¹¦Ð§£¬°üÀ¨Êý¾Ý±íÓ³Éä¡¢½ç˵¹ØÁª¹Øϵ¡¢Ê¹ÓÃÄ£×Ó¾ÙÐÐÊý¾Ý²Ù×÷ÒÔ¼°Ê¹Óûá¼ûÆ÷ºÍÐÞ¸ÄÆ÷×Ô½ç˵Êý¾Ý»á¼ûºÍÐ޸ġ£ÊìÁ·ÕÆÎÕÕâЩ¹¦Ð§£¬¿ÉÒÔÈÃÎÒÃÇÔ½·¢±ã½ÝµØÖÎÀíºÍ´¦ÀíÓ¦ÓóÌÐòÖеÄÊý¾Ý¡£
ÒÔÉϾÍÊÇlaravelµÄÄ£×Ó²ãÔÚÄĵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡