Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel EloquentʵÏÖÄ£×Ó¹ØÁª£¿
laravelÊÇÒ»¿îÊ¢ÐеÄphp¿ò¼Ü£¬ÆäÖаüÀ¨ÁËÇ¿Ê¢µÄorm£¨¹¤¾ß¹ØϵӳÉ䣩¿â¡ª¡ªlaravel eloquent¡£Õâ¸ö¿âºÜÊÇÇ¿Ê¢£¬¿ÉÒÔ×ÊÖúÎÒÃÇÇáËɵØʵÏÖÄ£×Ó¹ØÁª£¬´Ó¶øÔ½·¢Àû±ãµØÖÎÀíºÍÅÌÎÊÊý¾Ý¡£µ«Ðí¶à¿ª·¢ÕßÈ´²»ÖªµÀÔõÑùʹÓÃlaravel eloquentʵÏÖÄ£×Ó¹ØÁª¡£ÔÚ±¾ÎÄÖУ¬ÎÒ½«ÏÈÈÝÔõÑùʹÓÃlaravel eloquentʵÏÖÄ£×Ó¹ØÁª¡£
Ò»¡¢Laravel EloquentµÄ»ù±¾¿´·¨
ÔÚ×îÏÈÏÈÈÝÔõÑùʵÏÖÄ£×Ó¹ØÁªÖ®Ç°£¬ÎÒÃÇÒªÏÈÏàʶһЩLaravel EloquentµÄ»ù±¾¿´·¨¡£
1¡¢½¨ÉèÄ£×Ó
ÔÚLaravel EloquentÖУ¬ÎÒÃÇÐèҪΪÊý¾Ý¿âÖеÄÿһ¸ö±í½¨ÉèÒ»¸ö¶ÔÓ¦µÄÄ£×Ó£¬ÓÃÓÚ²Ù×÷ºÍÖÎÀíÕâ¸ö±íµÄÊý¾Ý¡£ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÔÚLaravelÖн¨ÉèÒ»¸öÄ£×Ó£º
php artisan make:model ModelName
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬ModelNameÊÇÄ£×ÓµÄÃû³Æ¡£´ËÏÂÁî»áÔÚappĿ¼Ï½¨ÉèÒ»¸öÓëÄ£×ÓÃû³ÆÏàͬµÄÀàÎļþ£¬Õâ¸öÎļþ¾ÍÊÇÎÒÃǵÄÄ£×Ó¡£
2¡¢±íÖ®¼äµÄ¹Øϵ
ÔÚÏÖʵ¿ª·¢ÖУ¬ÎÒÃǵÄÓ¦ÓúܿÉÄÜÉæ¼°µ½¶à¸ö±í£¬ÕâЩ±íÖ®¼ä¿ÉÄܱ£´æ׏ØÁª¹Øϵ¡£³£¼ûµÄ¹ØϵÀàÐÍÓÐÒ»¶ÔÒ»¹Øϵ¡¢Ò»¶Ô¶à¹Øϵ¡¢¶à¶Ô¶à¹ØϵµÈµÈ¡£
Ò»¶ÔÒ»¹Øϵ£ºÒ»¸öÄ£×Ó¶ÔÓ¦Ò»¸öÄ£×Ó£¬ÀýÈçÒ»¸öÓû§Ö»ÓÐÒ»¸öÉí·ÝÖ¤ºÅÂë¡£
Ò»¶Ô¶à¹Øϵ£ºÒ»¸öÄ£×Ó¶ÔÓ¦¶à¸öÄ£×Ó£¬ÀýÈçÒ»¸ö°à¼¶Óжà¸öѧÉú¡£
¶à¶Ô¶à¹Øϵ£º¶à¸öÄ£×ÓÖ®¼äÏ໥¹ØÁª£¬ÀýÈçѧÉúºÍ¿Î³ÌÖ®¼ä±£´æ¶à¶Ô¶à¹Øϵ¡£
3¡¢¹ØϵÀàÐÍ
ÔÚLaravel EloquentÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃһЩҪÁìÀ´ÊµÏÖ²î±ðµÄ¹ØϵÀàÐÍ¡£³£¼ûµÄ¹ØϵÀàÐÍ°üÀ¨belongsTo¡¢hasManyºÍbelongsToManyµÈ¡£
belongsTo£ºÓÃÓÚÒ»¶ÔÒ»ºÍÒ»¶Ô¶à¹ØϵÖеÄ×ÓÄ£×Ó£¬ÌåÏÖÄ¿½ñÄ£×ÓÊôÓÚÁíÒ»¸öÄ£×Ó¡£
hasMany£ºÓÃÓÚÒ»¶Ô¶à¹ØϵÖеĸ¸Ä£×Ó£¬ÌåÏÖÄ¿½ñÄ£×ÓÓµÓжà¸ö×ÓÄ£×Ó¡£
belongsToMany£ºÓÃÓÚ¶à¶Ô¶à¹Øϵ£¬ÌåÏÖÄ¿½ñÄ£×ÓÓëÁíÒ»¸öÄ£×Ó±£´æ¶à¶Ô¶à¹Øϵ¡£
¶þ¡¢ÔõÑùʹÓÃLaravel EloquentʵÏÖÄ£×Ó¹Øϵ
ÏàʶÍêLaravel EloquentµÄ»ù±¾¿´·¨Ö®ºó£¬ÎÒÃǾͿÉÒÔ×îÏÈѧϰÔõÑùʹÓÃLaravel EloquentʵÏÖÄ£×Ó¹ØϵÁË¡£ÏÂÃæÎÒ½«ÒÔÒ»¸ö¼òÆÓµÄÀý×ÓΪÀýÀ´ËµÃ÷ÔõÑùʵÏÖÄ£×Ó¹Øϵ¡£
¼ÙÉèÎÒÃÇÓÐÁ½¸ö±í£¬Ò»¸öÊÇstudent£¨Ñ§Éú£©±í£¬°üÀ¨µÄ×Ö¶ÎÓÐid£¨×ÔÔöÖ÷¼ü£©¡¢name£¨Ñ§ÉúÐÕÃû£©ºÍclass_id£¨ËùÊô°à¼¶ID£©£»ÁíÒ»¸öÊÇclass£¨°à¼¶£©±í£¬°üÀ¨µÄ×Ö¶ÎÓÐid£¨×ÔÔöÖ÷¼ü£©ºÍname£¨°à¼¶Ãû³Æ£©¡£ÎÒÃǵÄʹÃüÊÇÒª½¨ÉèÒ»¸öÒ»¶Ô¶àµÄ¹Øϵ£¬¼´Ò»¸ö°à¼¶¿ÉÄÜ°üÀ¨¶à¸öѧÉú¡£
1¡¢½¨ÉèÄ£×Ó
ÎÒÃÇÐèÒª»®·ÖΪstudentºÍclass±í½¨ÉèÄ£×Ó¡£ÔÚÖÕ¶ËÖÐÖ´ÐÐÒÔÏÂÁ½ÌõÏÂÁ
php artisan make:model Student php artisan make:model Class
µÇ¼ºó¸´ÖÆ
ÕâÑùÎÒÃǾÍÔÚappĿ¼ÏÂÌìÉúÁËStudentºÍClassÁ½¸öÄ£×Ó¡£
2¡¢ÔÚÄ£×ÓÖнç˵¹ØϵÀàÐͺ͹ØÁª±íÃû
ÔÚ½¨ÉèÍêÄ£×ÓÖ®ºó£¬ÎÒÃÇÐèÒªÔÚÄ£×ÓÖнç˵±íÖ®¼äµÄ¹ØϵÀàÐͺ͹ØÁª±íÃû¡£ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇÐèÒª½ç˵ClassÄ£×ÓµÄstudentsÒªÁìºÍStudentÄ£×ÓµÄclassÒªÁì¡£´úÂëÈçÏ£º
ClassÄ£×Ó£º
class Class extends Model { protected $table = "class"; public function students() { return $this->hasMany('AppStudent', 'class_id', 'id'); } }
µÇ¼ºó¸´ÖÆ
StudentÄ£×Ó£º
class Student extends Model { protected $table = "student"; public function class() { return $this->belongsTo('AppClass'); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓÃÁËhasManyºÍbelongsToÒªÁìÀ´½ç˵һ¶Ô¶à¹Øϵ¡£ÔÚhasManyÒªÁìÖУ¬µÚÒ»¸ö²ÎÊýÊÇ×ÓÄ£×ÓµÄÀàÃû£¬µÚ¶þ¸ö²ÎÊýÊÇ×ÓÄ£×ÓÖеÄÍâ¼ü£¬µÚÈý¸ö²ÎÊýÊǸ¸Ä£×ÓÖеÄÖ÷¼ü¡£×¢ÖØÕâÀïµÄ²ÎÊý˳Ðò²»¿Éµ¹Öá£
ÔÚbelongsToÒªÁìÖУ¬ÎÒÃÇͬÑùÐèÒªÖ¸¶¨¹ØÁªµÄÄ£×Ó£¬ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃǽ«¹ØÁªµÄÄ£×ÓÖ¸¶¨ÎªClassÄ£×Ó¡£±ðµÄ£¬ÎÒÃDz»ÐèÒªÖ¸¶¨×ÓÄ£×ÓÔÚ¸¸Ä£×ÓÖеÄÍâ¼ü£¬ÓÉÓÚLaravel»áƾ֤¹ØÁªÄ£×ÓµÄÃû³Æ×Ô¶¯ÍƲâÍâ¼üÃû³Æ¡£
3¡¢ÅÌÎʹØÁªÊý¾Ý
Íê³ÉÁËÉÏÊöµÄ²Ù×÷Ö®ºó£¬ÎÒÃǾͿÉÒÔÔÚ´úÂëÖÐÅÌÎʹØÁªÊý¾ÝÁË¡£ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏ´úÂëÅÌÎÊij¸ö°à¼¶µÄËùÓÐѧÉú£º
$class = Class::find(1); echo $class->name; $students = $class->students; foreach ($students as $student) { echo $student->name; }
µÇ¼ºó¸´ÖÆ
ÕâÑù£¬ÎÒÃǾͿÉÒÔÇáËɵØÅÌÎʳöij¸ö°à¼¶µÄËùÓÐѧÉúÁË¡£
ÔÚÕâ¸ö¼òÆÓµÄÀý×ÓÖУ¬ÎÒÃÇѧϰÁËÔõÑùʹÓÃLaravel EloquentʵÏÖÒ»¶Ô¶à¹Øϵ¡£ÐÅÍÐͨ¹ýÕâ¸öÀý×Ó£¬ÄãÒѾÏàʶÁËÔõÑù½ç˵±íÖ®¼äµÄ¹ØϵÀàÐÍ£¬ÒÔ¼°ÔõÑùÔÚ´úÂëÖÐÅÌÎʹØÁªÊý¾Ý¡£ÔÚÏÖʵ¿ª·¢ÖУ¬Laravel EloquentÉÐÓÐÐí¶àÇ¿Ê¢µÄÌØÕ÷£¬ÖµµÃÎÒÃÇÉîÈëѧϰºÍÓ¦Óá£
ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel EloquentʵÏÖÄ£×Ó¹ØÁª£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡