×ðÁú¿­Ê±¹ÙÍøµÇ¼

laravel ¹ØÁª ÅÌÎÊ´ÎÊý

laravelÊÇÒ»¸öºÜÊÇÊ¢ÐеÄphp¿ò¼Ü£¬ÓµÓи»ºñµÄÌØÕ÷ºÍÒ×ÓõÄapi¡£ÆäÖУ¬eloquent ormÊÇËü×îÇ¿Ê¢µÄÊý¾Ý¿â×é¼þÖ®Ò»£¬ÓÃËü¿ÉÒÔÇáËɵؾÙÐÐÅÌÎʺ͹ØÁª¡£

È»¶ø£¬µ±ÎÒÃǾÙÐÐÖØ´óµÄ¹ØÁªÅÌÎÊʱ£¬»á·¢Ã÷ÅÌÎʵĴÎÊý±äµÃÐí¶à£¬ÌØÊâÊÇÔÚʹÓÃÑ­»·Ê±¡£Õâ²»µ«»áÓ°ÏìÅÌÎÊÐÔÄÜ£¬»¹¿ÉÄܵ¼ÖÂÊý¾Ý¿âÅþÁ¬Òì³£¡£

ÄÇô£¬ÎÒÃǸÃÔõÑùïÔÌ­Laravel¹ØÁªÅÌÎʵĴÎÊýÄØ£¿ÏÂÃ棬±¾ÎĽ«ÌṩһЩ½â¾ö¼Æ»®¡£

ÑÓ³Ù¼ÓÔØ

ÔÚ¾ÙÐйØÁªÅÌÎÊʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃLaravelµÄÑÓ³Ù¼ÓÔع¦Ð§£¬Ëü»áÔÚÐèҪʹÓùØÁªÄ£×Óʱ²Å¾ÙÐÐÅÌÎÊ¡£

ÑÓ³Ù¼ÓÔØʹÓõÄÊÇħÊõÒªÁì__get()£¬ÒÔÊÇÖ»ÒªÔÚ¹ØÁªÄ£×ÓÉÏŲÓÃʱ£¬¾Í»á´¥·¢¹ØÁªÅÌÎÊ¡£

ÀýÈ磺

$users = User::all();

foreach ($users as $user) {

echo $user->profile->name;

µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ

}

ÈôÊÇÎÒÃÇÖ±½ÓÔÚforeachÑ­»·ÄÚʹÓÃ$users->profile£¬ÄÇôÔÚÿ´ÎÑ­»·ÄÚ£¬¶¼»áÖ´ÐÐÒ»´Î¹ØÁªÅÌÎÊ£¬ÕâÑù»áµ¼ÖÂÅÌÎÊ´ÎÊýÐí¶à£¬ÒÔÊÇÎÒÃÇ¿ÉÒÔʹÓÃÑÓ³Ù¼ÓÔØÀ´ÓÅ»¯Ëü¡£

$users = User::with(‘profile’)->get();

foreach ($users as $user) {

echo $user->profile->name;

µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ

}

ʹÓÃwith()ÒªÁìÔ¤¼ÓÔعØÁªÄ£×Óʱ£¬ÑÓ³Ù¼ÓÔØ»á×Ô¶¯ÆôÓã¬Ö»ÓÐÔÚÐèҪʹÓùØÁªÄ£×Óʱ²Å¾ÙÐÐÅÌÎÊ¡£

Eager Loading

ÑÓ³Ù¼ÓÔØÖ»ÒªÎÒÃÇÊÖ¶¯È¥Å²Óã¬ÈôÊÇÎÒÃÇûÓжԴúÂë×öÖÜÈ«µÄ¼ì²é£¬ÈÔÈ»»á·ºÆðÖظ´ÅÌÎʵÄÇéÐΣ¬ÒÔÊǸü³¹µ×µÄ½â¾öÒªÁìÊÇʹÓÃEager Loading£¨Ô¤¼ÓÔØ£©£¬Ëü¿ÉÒÔÒ»´ÎÐÔ¼ÓÔØËùÓÐÐèÒªµÄ¹ØÁªÄ£×Ó¡£

ÀýÈ磺

$users = User::with(‘profile’, ‘posts’)->get();

foreach ($users as $user) {

echo $user->profile->name;

foreach ($user->posts as $post) {
    echo $post->title;
}

µÇ¼ºó¸´ÖÆ

}

ʹÓÃwith()ÒªÁìͬʱ¼ÓÔضà¸ö¹ØÁªÄ£×Óʱ£¬»áÖ´Ðжà¸öSQLÅÌÎÊ£¬ÈôÊÇÎÒÃÇÖ»ÐèʹÓÃÆäÖÐÒ»¸ö¹ØÁªÄ£×Ó£¬ÄÇôÕâÖÖÒªÁì¾Í²»ÊÇ×îºÃµÄÑ¡Ôñ¡£

select ÉùÃ÷¹ØÁª×Ö¶Î

ÔÚEloquentÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃselectÒªÁì¶Ôij¸öÄ£×Ó¾ÙÐÐһЩɸѡ£¬Ö¸¶¨ÎÒÃÇÐèÒªµÄÌض¨ÁУ¬¶ø¹ØÁªÄ£×ÓÒ²ÊÇÒ»Ñù¡£

ÀýÈ磺

$users = User::with([‘profile’ => function ($query) {

$query->select('user_id', 'name');

µÇ¼ºó¸´ÖÆ

}])->get();

foreach ($users as $user) {

echo $user->profile->name; // Ö»»áÅÌÎÊ'profile'±íÖеÄ'user_id'ºÍ'name'ÁÐ

µÇ¼ºó¸´ÖÆ

}

ÔÚÕâ¸öÀý×ÓÖУ¬Í¨¹ýÖ¸¶¨select()ÒªÁìÊä³ö×Ô¼ºÐèÒªµÄÁУ¬¿ÉÒÔÈÃEloquent ORM Ò»´ÎÖ»ÅÌÎÊһЩÌض¨µÄÊý¾ÝÁУ¬ÕâÊÇÒ»¸ö¿ìËÙÅÌÎÊÌض¨Ãü¾ÝµÄºÃÒªÁ죬ҲΪÎÒÃÇïÔÌ­ÁËÌØÁíÍâÅÌÎÊ´ÎÊý£¬ÒÔÊÇÎÒÃÇ¿ÉÒÔʹÓÃÕâÖÖÒªÁìÀ´ÏÞÖƹØÁªÄ£×ÓµÄÅÌÎÊ¡£

ʹÓà join ¾ÙÐйØÁªÅÌÎÊ

³ýÁËÓë¹ØÁªÄ£×Ó´ò½»µÀÖ®Í⣬ÔÚijЩÌض¨ÇéÐÎÏ£¬joinÅÌÎÊ¿ÉÄܱÈEloquentµÄ¹ØÁªÅÌÎʸüÓÐÓá£

ÀýÈ磺

$users = DB::table(‘users’)

->join('profiles', 'users.id', '=', 'profiles.user_id')
->join('posts', 'users.id', '=', 'posts.user_id')
->select('users.name', 'profiles.age', 'posts.title')
->get();

µÇ¼ºó¸´ÖÆ

foreach ($users as $user) {

echo $user->name;
echo $user->age;
echo $user->title;

µÇ¼ºó¸´ÖÆ

}

ËäÈ»joinÅÌÎÊÔöÌíÁËSQLÓï¾äµÄÖØƯºóºÍ¿Éά»¤ÐÔ£¬µ«ÓëEloquent ORMÏà±È£¬joinÅÌÎÊ¿ÉÒÔ¸üºÃµØÓÅ»¯ÅÌÎÊÐÔÄÜ¡£

×ܽá

ÒÔÉϼ¸ÖÖÒªÁìÊÇÎÒÃÇ¿ÉÒÔÓÃÀ´ïÔÌ­Laravel¹ØÁªÅÌÎÊ´ÎÊýµÄÓÐÓü¼ÇÉ¡£Ò»Ñùƽ³£ÇéÐÎÏ£¬ÎÒÃÇ¿ÉÒÔʹÓÃÔ¤¼ÓÔغÍÑÓ³Ù¼ÓÔØ£¬ÒÔÓÅ»¯ÅÌÎÊÐÔÄÜ£¬²¢×èÖ¹Öظ´ÅÌÎÊ¡£¶ø¹ØÓÚһЩÌض¨ºÍÖØ´óµÄÅÌÎÊ£¬ÎÒÃÇ¿ÉÒÔ˼Á¿Ê¹ÓÃjoinÅÌÎÊ£¬ÒÔ»ñµÃ¸ü¸ßЧµÄЧ¹û¡£

×îºó£¬ÎÞÂÛÎÒÃǽÓÄÉÄÄÖÖ·½·¨£¬ÓÅ»¯ÅÌÎÊ×ÜÊǺÜÖ÷ÒªµÄ£¬Ëü¿ÉÒÔ´ó´óÌá¸ßÎÒÃǵÄÓ¦ÓÃÐÔÄÜ£¬²¢Ëõ¶ÌÎÒÃǵÄÏìӦʱ¼ä¡£

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

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

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

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

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

QR code
ÍøÕ¾µØͼ