×ðÁú¿­Ê±

laravelÔõôÁ¬±íÅÌÎʺϲ¢

ÔÚ laravel ÖУ¬ÎÒÃǾ­³£ÐèÒª¶Ô¶à¸ö±í¾ÙÐÐÁ¬±íÅÌÎÊ£¬ÒÔ±ã»ñÈ¡¸üΪÍêÕûµÄÊý¾ÝÐÅÏ¢£¬ÕâÒ²ÊÇ laravel Êý¾Ý¿â²Ù×÷µÄ½¹µã¹¦Ð§Ö®Ò»¡£µ«µ±ÐèÒª¶Ô¶à¸ö±í¾ÙÐÐÁ¬±íÅÌÎÊʱ£¬ÔõÑù¾ÙÐбí¹ØÁª³ÉΪÁËÒ»¸öÖ÷ÒªµÄÎÊÌâ¡£

Õë¶ÔÕâÒ»ÎÊÌ⣬ Laravel ÌṩÁ˶àÖÖ²î±ðµÄÁ¬±íÅÌÎÊ·½·¨£¬±¾ÎĽ«ÍŽáʵÀý£¬ÏÈÈÝÆäÖÐÒ»ÖÖ½ÏÁ¿³£ÓõÄÁ¬±íÅÌÎÊ·½·¨£ººÏ²¢Á¬±íÅÌÎÊ¡£

Ò»¡¢Ê²Ã´ÊǺϲ¢Á¬±íÅÌÎÊ

ÔÚ Laravel ÖУ¬ºÏ²¢Á¬±íÅÌÎÊʹÓõÄÊÇ Eloquent ORM ÌṩµÄ with() ÒªÁì¡£Õâ¸öÒªÁìÓÃÓÚÔÚÅÌÎÊÒ»¸öÄ£×Óʱ£¬Ô¤ÏȼÓÔØÓë¸ÃÄ£×ÓÏà¹ØÁªµÄÆäËûÄ£×ÓÊý¾Ý£¬´Ó¶ø×èֹƵÈÔÅÌÎÊÊý¾Ý¿âÔì³ÉµÄÐÔÄÜÎÊÌâ¡£

ÀýÈ磬ÏÖÔÚÓÐÁ½¸öÊý¾Ý±í»®·ÖΪ posts ºÍ comments£¬ÆäÖÐ posts ±í´æ´¢ÎÄÕµĻù±¾ÐÅÏ¢£¬comments ±í´æ´¢ÎÄÕµÄ̸ÂÛÐÅÏ¢£¬Á½¸ö±íµÄ¹ØϵÊÇÒ»¶Ô¶àµÄ¹Øϵ£¬Ò²¾ÍÊÇÒ»¸öÎÄÕ¶ÔÓ¦¶à¸ö̸ÂÛ¡£

Ê×ÏÈÎÒÃÇÐèÒª½ç˵ Post ºÍ Comment Á½¸öÄ£×Ó£º

// Post Ä£×ÓÖнç˵Óë comments ±íµÄÒ»¶Ô¶à¹Øϵ
class Post extends Model
{
    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}

// Comment Ä£×ÓÖнç˵Óë posts ±íµÄ¹ØÁª¹Øϵ
class Comment extends Model
{
    public function post()
    {
        return $this->belongsTo(Post::class);
    }
}

µÇ¼ºó¸´ÖÆ

½ÓÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÏÂÃæµÄÒªÁìʹÓà Laravel Eloquent ORM ¾ÙÐÐÅÌÎÊ£º

$posts = Post::with('comments')->get();

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄ´úÂëÖУ¬ with(‘comments’) ÌåÏÖ¼ÓÔØÓëÎÄÕÂÏà¹ØÁªµÄ̸ÂÛÊý¾Ý¡£ÓÉÓÚ comments Óë posts ÊÇÒ»¶Ô¶àµÄ¹Øϵ£¬Òò´ËʹÓà hasMany() ÒªÁì½ç˵ÁË posts Óë comments Ö®¼äµÄ¹Øϵ£¬Ê¹Óà belongsTo() ÒªÁì½ç˵ÁË comments Óë posts Ö®¼äµÄ¹Øϵ¡£

Ö´ÐÐÒÔÉÏ´úÂëºó£¬ÎÒÃÇ»ñµÃµÄÊÇÒ»¸ö°üÀ¨ËùÓÐÎÄÕ¼°Æä̸ÂÛÐÅÏ¢µÄÜöÝÍ¡£ÆäÖÐÿ¸öÎÄÕ嵀 comments ÊôÐÔÊÇÒ»¸ö°üÀ¨Æä¶ÔӦ̸ÂÛÐÅÏ¢µÄÜöÝÍ¡£

¶þ¡¢ºÏ²¢Á¬±íÅÌÎʵÄÓÅÊÆ

ÓëÆäËûÁ¬±íÅÌÎÊ·½·¨Ïà±È£¬ºÏ²¢Á¬±íÅÌÎÊÓÐÒÔÏÂÓÅÊÆ£º

1¡¢ÌáÉýÅÌÎÊЧÂÊ£ººÏ²¢Á¬±íÅÌÎÊ¿ÉÒÔ×èÖ¹¶à´ÎÅÌÎÊÊý¾Ý¿âÔì³ÉµÄÐÔÄÜÎÊÌ⣬Ìá¸ßÅÌÎÊЧÂÊ¡£

2¡¢Êý¾ÝÒ»ÖÂÐÔ£ºµ±Ê¹Óöà´ÎÅÌÎÊÊý¾Ý¿âʱ£¬Êý¾ÝµÄÒ»ÖÂÐÔ¿ÉÄÜ»áÊܵ½Ó°Ï죬¶øʹÓúϲ¢Á¬±íÅÌÎÊ¿ÉÒÔ°ü¹ÜÊý¾ÝµÄÒ»ÖÂÐÔ¡£

3¡¢½ÚÔ¼´úÂëÁ¿£ºÊ¹Óà Eloquent ORM µÄ with() ÒªÁì¿ÉÒÔÔÚÒ»ÐдúÂëÖÐÍê³É¶à¸öÊý¾Ý±íµÄÅÌÎÊ£¬½ÚÔ¼´úÂëÁ¿¡£

Èý¡¢ºÏ²¢Á¬±íÅÌÎʵÄʹÓó¡¾°

ºÏ²¢Á¬±íÅÌÎÊͨ³£ÊÊÓÃÓÚÒÔϳ¡¾°£º

1¡¢Ò»¸öÄ£×ÓÐèÒª¼ÓÔضà¸öÏà¹ØÁªµÄÄ£×ÓÊý¾Ý¡£

2¡¢¶à¸öÄ£×ÓÖ®¼äµÄÊý¾Ý¹Øϵ½ÏÁ¿ÖØ´ó£¬Ê¹Óöà´ÎÅÌÎʻήµÍ´úÂë¿É¶ÁÐÔ¡£

3¡¢ÐèÒª¶Ô¶à¸öÏà¹ØÁªµÄÊý¾Ý±í¾ÙÐÐɸѡ¡¢ÅÅÐò¡¢·ÖÒ³µÈ²Ù×÷¡£

ËÄ¡¢ºÏ²¢Á¬±íÅÌÎʵÄ×¢ÖØÊÂÏî

ÔÚʹÓúϲ¢Á¬±íÅÌÎÊʱ£¬ÐèҪעÖØÒÔÏÂÎÊÌ⣺

1¡¢ÅÌÎʵÄÊý¾Ý±íÖбØÐè±£´æÍâ¼ü¹ØÁª£¬²»È»ÎÞ·¨¾ÙÐÐÅÌÎÊ¡£

2¡¢Ê¹Óà with() ÒªÁìʱ£¬¿ÉÒÔÒ»´Î¼ÓÔضà¸ö¹ØÁªÄ£×Ó£¬ÀýÈ磺

$posts = Post::with('comments', 'tags')->get();

µÇ¼ºó¸´ÖÆ

3¡¢ÐèҪעÖØ×èֹʹÓÃǶÌ×ÅÌÎÊÓï¾ä£¬Õâ¿ÉÄÜ»áÔì³ÉÖظ´ÅÌÎʵÈÎÊÌâ¡£

4¡¢ÅÌÎʵÄÊý¾ÝÁ¿½Ï´óʱ£¬Ðè×¢ÖØÅÌÎÊЧÂʺÍÄÚ´æÕ¼ÓÃÇéÐΡ£

Îå¡¢×ܽá

ÔÚ Laravel ÖУ¬Á¬±íÅÌÎÊÊÇÒ»ÏîºÜÖ÷ÒªµÄÊý¾Ý¿â²Ù×÷£¬ÎªÎÒÃÇÌṩÁ˱ã½ÝµÄÊý¾Ý´¦Öóͷ£·½·¨¡£¶øºÏ²¢Á¬±íÅÌÎÊ×÷ΪÆäÖÐÒ»ÖÖ½ÏÁ¿³£¼ûµÄ·½·¨£¬²»µ«Äܹ»Ìá¸ßÅÌÎÊЧÂÊ£¬»¹Äܹ»°ü¹ÜÊý¾ÝÒ»ÖÂÐÔ£¬Í¬Ê±´úÂë¿É¶ÁÐÔÒ²¸ü¸ß¡£Í¨¹ý±¾ÎĵÄÏÈÈÝ£¬ÐÅÍжÁÕßÒѾ­¶Ô Laravel ÖÐÁ¬±íÅÌÎʵÄʹÓÃÒªÁìÓÐÁËÒ»¶¨µÄÏàʶ£¬Ï£Íû¶Ô¸÷ÈËÔÚÏÖʵ¿ª·¢ÖÐÓÐËù×ÊÖú¡£

ÒÔÉϾÍÊÇlaravelÔõôÁ¬±íÅÌÎʺϲ¢µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±ÊµÊ±ÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
¡¾ÍøÕ¾µØͼ¡¿¡¾sitemap¡¿