laravelÅÌÎÊÖ¸¶¨×Ö¶Î
laravelÊÇÒ»¸öºÜÊÇÊ¢ÐеÄphp¿ò¼Ü£¬¿ÉÒÔ×ÊÖú¿ª·¢Õß¿ìËÙ¹¹½¨webÓ¦ÓóÌÐò¡£ÆäÖУ¬Êý¾Ý¿âÅÌÎÊÊÇ¿ò¼ÜµÄÖ÷Òª×é³É²¿·Ö¡£ÔÚlaravelÖУ¬ÅÌÎÊÊý¾Ý¿â¿ÉÒÔʹÓÃÅÌÎʹ¹½¨Æ÷¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃlaravelÅÌÎʹ¹½¨Æ÷À´ÅÌÎÊÖ¸¶¨×ֶΡ£
ÅÌÎÊËùÓÐ×Ö¶Î
ÔÚLaravelÖУ¬ÈôÊÇÎÒÃÇÏëÅÌÎÊÒ»¸ö±íÖеÄËùÓÐ×ֶΣ¬¿ÉÒÔÖ±½ÓʹÓÃÒÔÏÂÅÌÎʹ¹½¨Æ÷ÒªÁ죺
$data = DB::table('table_name')->get();
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬table_nameÊÇÒªÅÌÎʵıíÃû£¬get()ÒªÁìÌåÏÖÒª»ñÈ¡±íÖеÄËùÓÐÊý¾Ý¡£
ÅÌÎÊÖ¸¶¨×Ö¶Î
ÈôÊÇÎÒÃÇÖ»ÏëÅÌÎʱíÖеÄÌض¨×ֶΣ¬¿ÉÒÔʹÓÃselect()ÒªÁìÀ´Ñ¡ÔñÐèÒªµÄ×ֶΡ£ÏêϸÓï·¨ÈçÏ£º
$data = DB::table('table_name') ->select('field1', 'field2', ...) ->get();
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬field1¡¢field2µÈÊÇÎÒÃÇÏëÒªÅÌÎʵÄ×Ö¶ÎÃû£¬¶à¸ö×Ö¶ÎÃû¿ÉÒÔʹÓöººÅÍÑÀë¡£
ÅÌÎʶà¸ö±íÖеÄÖ¸¶¨×Ö¶Î
ÔÚLaravelÖУ¬¶à¸ö±íÖ®¼ä¿ÉÒÔ¾ÙÐйØÁªÅÌÎÊ¡£ÈôÊÇÎÒÃÇÏëÅÌÎʶà¸ö±íµÄÖ¸¶¨×ֶΣ¬¿ÉÒÔʹÓÃjoin()ÒªÁìÀ´¾ÙÐйØÁªÅÌÎÊ¡£ÏêϸÓï·¨ÈçÏ£º
$data = DB::table('table1') ->leftJoin('table2', 'table1.id', '=', 'table2.table1_id') ->select('table1.field1', 'table2.field2', ...) ->get();
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬table1ºÍtable2ÊÇÎÒÃÇÒªÅÌÎʵÄÁ½ÕÅ±í£¬leftJoin()ÒªÁìÌåÏÖʹÓÃ×óÅþÁ¬¹ØÁªÕâÁ½¸ö±í£¬table1.idºÍtable2.table1_idÊǹØÁªÌõ¼þ£¬select()ÒªÁìÒÀÈ»ÊÇÓÃÀ´Ñ¡ÔñÐèÒªÅÌÎʵÄ×ֶΡ£
ʹÓÃÄ£×ÓÀàÅÌÎÊÖ¸¶¨×Ö¶Î
³ýÁËʹÓÃÅÌÎʹ¹½¨Æ÷£¬ÎÒÃÇ»¹¿ÉÒÔʹÓÃÄ£×ÓÀàÀ´ÅÌÎÊÊý¾Ý¡£Ê×ÏÈÒª½¨ÉèÄ£×ÓÀ࣬Ȼºó¿ÉÒÔʹÓÃselect()ÒªÁìÀ´ÅÌÎÊÖ¸¶¨×ֶΡ£ÏêϸÓï·¨ÈçÏ£º
class MyModel extends Model { protected $table = 'table_name'; protected $fillable = ['field1', 'field2', ...]; } $data = MyModel::select('field1', 'field2', ...) ->get();
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬MyModelÊÇÎÒÃǽ¨ÉèµÄÄ£×ÓÀ࣬table_nameÊÇÓëÄ£×Ó¶ÔÓ¦µÄ±íÃû£¬$fillableÊÇ¿ÉÒÔ¾ÙÐÐÅúÁ¿¸³ÖµµÄ×ֶΡ£
×ܽá
ÒÔÉϾÍÊÇʹÓÃLaravelÅÌÎʹ¹½¨Æ÷ÅÌÎÊÖ¸¶¨×ֶεÄÒªÁì¡£¹ØÓÚÊý¾Ý±íÖÐ×ֶν϶àµÄÇéÐΣ¬ÎÒÃÇ¿ÉÒÔÖ»ÅÌÎÊÐèÒªµÄ×ֶΣ¬ïÔ̲»ÐëÒªµÄÅÌÎʺÍ×ÊÔ´ãýÃð¡£Ê¹ÓÃÄ£×ÓÀàÅÌÎÊÊý¾ÝÒ²ÊÇÒ»ÖÖ¼òÆÓ¸ßЧµÄÅÌÎÊ·½·¨¡£ÔÚÏÖʵ¿ª·¢ÖУ¬¿ÉÒÔƾ֤ÏêϸÇéÐÎÑ¡ÔñºÏÊʵÄÅÌÎÊÒªÁì¡£
ÒÔÉϾÍÊÇlaravelÅÌÎÊÖ¸¶¨×ֶεÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡