尊龙凯时人生就是博

laravelÔõÑù¶à±íÁª²é

ÔÚlaravel¿ª·¢ÖУ¬¶à±íÁª²éÊdz£¼ûµÄ²Ù×÷¡£Áª²é¿ÉÒÔʹ¶à¸ö±íµÄÊý¾Ýƾ֤Ìض¨Ìõ¼þ¾ÙÐÐ×éºÏ£¬²¢·µ»ØËùÐèÊý¾ÝµÄЧ¹û¼¯¡£ÔÚlaravelÖÐʵÏÖ¶à±íÁª²éÐèÒªÓõ½eloquent ormÌṩµÄÇ¿Ê¢¹¦Ð§¡£±¾ÎĽ«»áÏÈÈÝÔõÑùʹÓÃlaravel¶à±íÁª²é¡£

Eloquent ORM

LaravelµÄEloquent ORMÊÇÒ»ÖÖ¹¤¾ß¹ØϵӳÉ䣨ORM£©ÊÖÒÕ£¬ËüÌṩÁËÒ»¸öÎÞа¡¢¼òÆӵķ½·¨À´»á¼ûºÍ²Ù×÷Êý¾Ý¿â¡£Eloquent ORMͨ¹ý½«Êý¾Ý¿â±íÓ³Éäµ½¹¤¾ßÀ´ÊµÏÖÕâһĿµÄ¡£ÕâЩ¹¤¾ß¿ÉÒÔͨ¹ýPHP´úÂëÀ´²Ù×÷£¬²¢½«Æä״̬³¤ÆÚÉúÑĵ½Êý¾Ý¿âÖС£ORMµÄÖ÷ÒªÓŵãÊÇËü½«ÖØ´óµÄSQLÓï¾äת»»Îª¼òÆӵŤ¾ßÒªÁìŲÓã¬Òò´Ë£¬¿ª·¢Õß¿ÉÒÔרעÓÚ´úÂëÂß¼­£¬¶ø²»ÊÇSQLÓï¾ä¡£

¶à±íÅÌÎÊ

LaravelÌṩÁ˶àÖÖÒªÁìÀ´ÊµÏÖ¶à±íÁª²é¡£ÆäÖУ¬×î³£¼ûµÄÒªÁìÊÇͨ¹ýʹÓÃEloquent¹ØÁªÐÔʵÏÖ¡£ÓÉÓÚLaravelµÄEloquent ORMÌṩÁËÇ¿Ê¢µÄ¹ØÁªÐÔ¹¦Ð§£¬Òò´ËÔÚ¶à±íÅÌÎÊÖÐʹÓÃEloquent¹ØÁªÊǺÜÊÇÀû±ãµÄ¡£ÏÂÃæÊÇһЩ³£ÓõĶà±íÅÌÎÊÒªÁì¡£

2.1 Ò»¶ÔÒ»¹ØÁª

Ò»¶ÔÒ»¹ØÁªÊÇÖ¸Á½¸ö±íÖ®¼äÖ»ÓÐÒ»¸öÆ¥ÅäµÄÐС£ÔÚLaravelÖÐʵÏÖÒ»¶ÔÒ»¹ØÁªºÜÊǼòÆÓ£¬Ö»ÐèÔÚÄ£×ÓÖнç˵hasOne»òbelongsToÒªÁì¼´¿É¡£

ÀýÈ磬˼Á¿ÒÔÏÂÁ½¸ö±í£º

users

id | name | email | password | created_at | updated_at

profiles

id | user_id | phone | address | created_at | updated_at

Òª²éÕÒÒ»¸öÓû§µÄСÎÒ˽ÈË×ÊÁÏ£¬¿ÉÒÔÔÚUserÄ£×ÓÖнç˵һ¸öhasOne¹Øϵ£º

class User extends Model

{

public function profile()
{
    return $this->hasOne('AppModelsProfile');
}

µÇ¼ºó¸´ÖÆ

}

È»ºó£¬ÔÚProfileÄ£×ÓÖнç˵һ¸öbelongsToÒªÁìÀ´Ö¸¶¨¹ØÁª£º

class Profile extends Model

{

public function user()
{
    return $this->belongsTo('AppModelsUser');
}

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

}

ÏÖÔÚ¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´»ñÈ¡Óû§ÒÔ¼°ËûµÄСÎÒ˽ÈË×ÊÁÏ£º

$user = User::with(‘profile’)->find(1);

ÔÚÕâ¸öÀý×ÓÖУ¬withÒªÁìÓÃÀ´Ö´ÐÐEager Loading¡£Õ⽫ÔÚÒ»´ÎÅÌÎÊÖлñÈ¡Óû§ÒÔ¼°ÓëÖ®Ïà¹ØµÄСÎÒ˽ÈË×ÊÁÏ£¬´Ó¶ø×èÖ¹Á˶àÓàµÄÅÌÎÊ¡£findÒªÁìÓÃÀ´²éÕÒÖ¸¶¨IDµÄÓû§¡£×¢ÖØ£¬½ç˵¹ØÁªÊ±£¬EloquentĬÈϻὫÍâ¼üÃüÃûΪ¹ØÁªÄ£×ÓµÄÃû³Æ+¡°_id¡±£¬ÔÆÔÆÀýÖеġ°user_id¡±×ֶΡ£

2.2 Ò»¶Ô¶à¹ØÁª

Ò»¶Ô¶à¹ØÁªÊÇÖ¸Ò»¸ö±íÖеÄÒ»ÐпÉÒÔ¹ØÁªµ½ÁíÒ»¸ö±íÖеĶà¸öÐС£ÔÚLaravelÖÐʵÏÖÒ»¶Ô¶à¹ØÁªºÜÊǼòÆÓ£¬Ö»ÐèÔÚÄ£×ÓÖнç˵hasMany»òbelongsToÒªÁì¼´¿É¡£

ÀýÈ磬˼Á¿ÒÔÏÂÁ½¸ö±í£º

users

id | name | email | password | created_at | updated_at

posts

id | user_id | title | body | created_at | updated_at

Òª²éÕÒÒ»¸öÓû§µÄËùÓÐÎÄÕ£¬¿ÉÒÔÔÚUserÄ£×ÓÖнç˵һ¸öhasMany¹Øϵ£º

class User extends Model

{

public function posts()
{
    return $this->hasMany('AppModelsPost');
}

µÇ¼ºó¸´ÖÆ

}

È»ºó£¬ÔÚPostÄ£×ÓÖнç˵һ¸öbelongsToÒªÁìÀ´Ö¸¶¨¹ØÁª£º

class Post extends Model

{

public function user()
{
    return $this->belongsTo('AppModelsUser');
}

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

}

ÏÖÔÚ¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´»ñÈ¡Óû§ÒÔ¼°ËûµÄËùÓÐÎÄÕ£º

$user = User::with(‘posts’)->find(1);

ÔÚÕâ¸öÀý×ÓÖУ¬Eloquent½«×Ô¶¯²éÕÒuser_id¼´ÊÇÄ¿½ñÓû§IDµÄËùÓÐÎÄÕ£¬²¢½«ËüÃÇÉèÖÃΪÓû§Ä£×ÓµÄpostsÊôÐÔ¡£×¢ÖØ£¬½ç˵¹ØÁªÊ±£¬EloquentĬÈϻὫÍâ¼üÃüÃûΪ¹ØÁªÄ£×ÓµÄÃû³Æ+¡°_id¡±£¬ÔÆÔÆÀýÖеġ°user_id¡±×ֶΡ£

2.3 ¶à¶Ô¶à¹ØÁª

¶à¶Ô¶à¹ØÁªÊÇÖ¸Á½¸ö±íÖ®¼ä¿ÉÒÔÓжà¸öÆ¥ÅäµÄÐС£ÔÚLaravelÖÐʵÏÖ¶à¶Ô¶à¹ØÁªÐèҪʹÓÃbelongsToManyÒªÁì¡£

ÀýÈ磬˼Á¿ÒÔÏÂÁ½¸ö±í£º

users

id | name | email | password | created_at | updated_at

roles

id | name | label | created_at | updated_at

ÖÐÐıíroles_users

id | user_id | role_id

Òª²éÕÒËùÓоßÓÐÖ¸¶¨½ÇÉ«µÄÓû§£¬¿ÉÒÔÔÚRoleÄ£×ÓÖнç˵һ¸öbelongsToMany¹Øϵ£º

class Role extends Model

{

public function users()
{
    return $this->belongsToMany('AppModelsUser');
}

µÇ¼ºó¸´ÖÆ

}

È»ºó£¬ÔÚUserÄ£×ÓÖÐÒ²½ç˵һ¸öbelongsToManyÒªÁìÀ´Ö¸¶¨¹ØÁª£º

class User extends Model

{

public function roles()
{
    return $this->belongsToMany('AppModelsRole');
}

µÇ¼ºó¸´ÖÆ

}

ÏÖÔÚ¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´»ñÈ¡ËùÓоßÓÐÖ¸¶¨½ÇÉ«µÄÓû§£º

$users = Role::with(‘users’)->where(‘name’, ‘admin’)->get();

ÔÚÕâ¸öÀý×ÓÖУ¬withÒªÁìÓÃÀ´Ö´ÐÐEager Loading£¬getÒªÁìÓÃÀ´»ñÈ¡ËùÓоßÓÐÖ¸¶¨½ÇÉ«µÄÓû§¡£×¢ÖØ£¬¶à¶Ô¶à¹ØÁªÐèÒªÖÐÐıíµÄ±£´æ£¬ËüÓÃÓÚ½«Á½¸ö±íÖ®¼äµÄ¹ØϵÉúÑĵ½Êý¾Ý¿âÖС£

×ܽá

LaravelÊÇÒ»¸ö¹¦Ð§Ç¿Ê¢µÄWeb¿ª·¢¿ò¼Ü£¬ËüÌṩÁËÐí¶àÀû±ãµÄ¹¦Ð§À´¼ò»¯¿ª·¢Á÷³Ì£¬ÆäÖÐÖ®Ò»¾ÍÊÇEloquent ORM¡£Eloquent ORMÌṩÁ˼òÆÓ¶øÓÐÁ¦µÄÒªÁìÀ´´¦ÀíÊý¾Ý¿â£¬°üÀ¨¶à±íÁª²é¡£ÈôÊÇÎÒÃÇÊìϤʹÓÃLaravelµÄEloquent¹ØÁªÐÔ£¬ÊµÏÖ¶à±íÁª²é¾ÍÊÇÒ»¼þ¼òÆÓµÄÊÂÇé¡£

ÒÔÉϾÍÊÇlaravelÔõÑù¶à±íÁª²éµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是博

13452372176

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

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

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