laravelÔõô×èÖ¹foreach²é±í
ÔÚlaravelÖÐʹÓùØϵӳÉ䣨relationship mapping£©¿ÉÒÔ×èֹʹÓÃforeach²é±íµÄÎÊÌâ¡£¹ØϵӳÉäÊÇlaravelÇ¿Ê¢µÄorm£¨object-relational mapping£©ÌØÕ÷Ö®Ò»£¬Ëü¿ÉÒÔ×Ô¶¯µØΪģ×ÓÖÐµÄ±í½¨Éè¹ØÁª¹Øϵ¡£
ÔÚLaravelÖУ¬ÓÐÈýÖÖ¹ØϵӳÉ䣺һ¶ÔÒ»£¨One-to-one£©¡¢Ò»¶Ô¶à£¨One-to-many£©ºÍ¶à¶Ô¶à£¨Many-to-many£©¡£ÕâЩ¹ØϵӳÉä¿ÉÒÔͨ¹ýEloquentÄ£×ÓÀ´½ç˵ºÍ½¨Éè¡£ÏÂÃæÎÒÃǽ«ÔÚʵ¼ùµÄ»ù´¡ÉÏ£¬ÏêϸÏÈÈÝÕâÈýÖÖ¹ØϵӳÉäµÄÓ÷¨ºÍʵÏÖ¡£
Ò»¶ÔÒ»¹ØϵӳÉä
Ò»¶ÔÒ»¹ØϵӳÉä¾³£ÓÃÓÚÁ½¸ö±í¸ñ¹²ÓÃͳһ¸öÖ÷¼üµÄÇéÐÎÏ£¬ÆäÖÐÒ»¸ö±í¸ñÖеÄÒ»ÐÐÊý¾ÝÖ»ÄܶÔÓ¦ÁíÒ»¸ö±í¸ñÖеÄÒ»ÐÐÊý¾Ý¡£ÀýÈ磬¶©µ¥±íºÍ¿Í»§±í¡£Ã¿¸ö¶©µ¥¶¼Ö»ÊôÓÚÒ»¸ö¿Í»§£¬Ã¿¸ö¿Í»§Ò²Ö»ÓÐÒ»¸ö¶©µ¥¡£ÕâÖÖÇéÐÎÏ£¬¶©µ¥±íµÄ¿Í»§IDÁоÍÊÇ¿Í»§±íÖеÄÖ÷¼üÁС£
ÔÚEloquentÖУ¬ÎÒÃÇ¿ÉÒÔ½ç˵Á½¸öÄ£×Ó£º¶©µ¥Ä£×ӺͿͻ§Ä£×Ó£¬²¢½¨ÉèÕâÁ½¸öÄ£×ÓÖ®¼äµÄÒ»¶ÔÒ»¹ØϵӳÉ䣺
// ¶©µ¥Ä£×Ó class Order extends Model { public function customer() { return $this->hasOne(Customer::class); } } // ¿Í»§Ä£×Ó class Customer extends Model { public function order() { return $this->belongsTo(Order::class); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇ»®·ÖÔÚOrderºÍCustomerÄ£×ÓÖнç˵ÁËÒ»¸ö¹ØϵҪÁ죬»®·ÖΪcustomerºÍorder¡£ÕâÁ½¸ö¹ØϵҪÁ춼ʹÓÃÁËEloquentÄ£×ÓÖеÄhasOneºÍbelongsToÒªÁ죬À´½¨ÉèÕâÁ½¸öÄ£×ÓÖ®¼äµÄÒ»¶ÔÒ»¹ØϵӳÉä¡£
µ±ÎÒÃÇÐèÒª²éÕÒij¸ö¶©µ¥µÄ¿Í»§ÐÅϢʱ£¬Ö»ÐèҪͨ¹ý$order->customerÒªÁì¼´¿ÉʵÏÖ£º
$order = Order::find($id); $customer = $order->customer;
µÇ¼ºó¸´ÖÆ
Ò»¶Ô¶à¹ØϵӳÉä
Ò»¶Ô¶à¹ØϵӳÉä¾³£ÓÃÓÚÒ»¸ö±í¸ñÖеÄÒ»ÐÐÊý¾Ý¿ÉÒÔ¶ÔÓ¦¶à¸ö±í¸ñÖеÄÐÐÊý¾Ý¡£ÀýÈ磬һ¸öÓû§ÓжàƪÎÄÕ£¬»òÕßÒ»¸ö²¿·ÖÓжà¸öÔ±¹¤¡£ÕâÖÖÇéÐÎÏ£¬ÎÒÃÇÐèÒªÔÚÏà¹ØµÄEloquentÄ£×ÓÖУ¬Ê¹ÓÃhasManyºÍbelongsToÒªÁìÀ´½¨ÉèÒ»¶Ô¶à¹ØϵӳÉä¡£
ÒÔÓû§±íºÍÎÄÕ±íΪÀý£¬ÎÒÃÇÐèÒªÔÚUserºÍArticleÄ£×ÓÖн¨ÉèÒ»¶Ô¶à¹ØϵӳÉ䣺
// Óû§Ä£×Ó class User extends Model { public function articles() { return $this->hasMany(Article::class); } } // ÎÄÕÂÄ£×Ó class Article extends Model { public function user() { return $this->belongsTo(User::class); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇÔÚUserÄ£×ÓÖнç˵ÁËÒ»¸öÃûΪarticlesµÄ¹ØϵҪÁ죬ͨ¹ýhasManyÒªÁìʵÏÖÁËÓû§ºÍÎÄÕÂÖ®¼äµÄÒ»¶Ô¶à¹ØϵӳÉä¡£ÔÚArticleÄ£×ÓÖнç˵ÁËÒ»¸öÃûΪuserµÄ¹ØϵҪÁ죬ͨ¹ýbelongsToÒªÁìʵÏÖÁËÎÄÕºÍÓû§Ö®¼äµÄ¶à¶ÔÒ»¹ØϵӳÉä¡£
µ±ÎÒÃÇÐèÒª²éÕÒij¸öÓû§µÄËùÓÐÎÄÕÂʱ£¬Ö»Ðèͨ¹ý$user->articlesÒªÁìʵÏÖ£º
$user = User::find($id); $articles = $user->articles;
µÇ¼ºó¸´ÖÆ
¶à¶Ô¶à¹ØϵӳÉä
¶à¶Ô¶à¹ØϵӳÉä¾³£ÓÃÓÚ±£´æÁ½¸ö±í¸ñÖ®¼ä¶à¶Ô¶àµÄ¹Øϵ¡£ÀýÈ磬Óû§ºÍ½ÇÉ«µÄ¹Øϵ£¬Ò»¸öÓû§¿ÉÒÔÓµÓжà¸ö½ÇÉ«£¬Ò»¸ö½ÇÉ«Ò²¿ÉÒÔ±»¶à¸öÓû§ËùÓµÓС£ÕâÖÖÇéÐÎÏ£¬ÎÒÃÇÐèÒªÔÚEloquentÄ£×ÓÖÐʹÓÃbelongsToManyÒªÁìÀ´½¨Éè¶à¶Ô¶à¹ØϵӳÉä¡£
ÒÔÓû§±íºÍ½ÇÉ«±íΪÀý£¬ÎÒÃÇÐèÒªÔÚUserºÍRoleÄ£×ÓÖн¨Éè¶à¶Ô¶à¹ØϵӳÉ䣺
// Óû§Ä£×Ó class User extends Model { public function roles() { return $this->belongsToMany(Role::class); } } // ½ÇÉ«Ä£×Ó class Role extends Model { public function users() { return $this->belongsToMany(User::class); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇÔÚUserÄ£×ÓÖнç˵ÁËÒ»¸öÃûΪrolesµÄ¹ØϵҪÁ죬ͨ¹ýbelongsToManyÒªÁìʵÏÖÁËÓû§ºÍ½ÇÉ«Ö®¼äµÄ¶à¶Ô¶à¹ØϵӳÉä¡£ÔÚRoleÄ£×ÓÖнç˵ÁËÒ»¸öÃûΪusersµÄ¹ØϵҪÁ죬ͨ¹ýbelongsToManyÒªÁìʵÏÖÁ˽ÇÉ«ºÍÓû§Ö®¼äµÄ¶à¶Ô¶à¹ØϵӳÉä¡£
µ±ÎÒÃÇÐèÒª²éÕÒij¸öÓû§µÄËùÓнÇɫʱ£¬Ö»Ðèͨ¹ý$user->rolesÒªÁìʵÏÖ£º
$user = User::find($id); $roles = $user->roles;
µÇ¼ºó¸´ÖÆ
×ܽá
ÔÚLaravelÖÐʹÓùØϵӳÉäÌØÕ÷¿ÉÒÔÓÐÓõØ×èֹʹÓÃforeach²é±íµÄÎÊÌâ¡£Ò»¶ÔÒ»¡¢Ò»¶Ô¶àºÍ¶à¶Ô¶à¹ØϵӳÉä»®·Ö¶ÔÓ¦Á˲î±ðµÄÊý¾Ý±íÖ®¼äµÄ¹Øϵ£¬Í¨¹ýEloquentÄ£×ÓÒªÁì¾ÙÐнç˵ºÍʵÏÖ¡£ÎÒÃÇ¿ÉÒÔͨ¹ýÔÚÏà¹ØµÄEloquentÄ£×ÓÖнç˵¹ØϵҪÁ죬ʹÓÃhasMany¡¢belongsToºÍbelongsToManyÒªÁìÀ´½¨Éè¹ØϵӳÉä¡£ÕâÖÖ·½·¨Ê¹µÃÎÒÃÇÄܹ»¼òÆÓ¡¢Àû±ãµØʵÏÖÖØ´óµÄÊý¾Ý¿â²Ù×÷£¬Í¬Ê±Ò²Ê¹µÃÎÒÃǵĴúÂëÔ½·¢ÇåÎú¡¢Ò×ÓÚά»¤¡£
ÒÔÉϾÍÊÇlaravelÔõô×èÖ¹foreach²é±íµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡