尊龙凯时人生就是搏

laravelÅÌÎÊд¶à¸öÌõ¼þ

ÔÚlaravelÖÐ £¬ÎÒÃǾ­³£ÐèҪƾ֤¶à¸öÌõ¼þ¾ÙÐÐÅÌÎÊ¡£ÕâЩÌõ¼þ¿ÉÄÜ°üÀ¨×Ö¶ÎÖµ¡¢Ê±¼ä¹æÄ£¡¢¹ØϵµÈµÈ¡£ÔÚ±¾ÎÄÖÐ £¬ÎÒÃǽ«ÌÖÂÛÔõÑùʹÓÃlaravel eloquent¾ÙÐжàÌõ¼þÅÌÎÊ¡£

»ù±¾ÅÌÎÊ

ÔÚLaravelÖоÙÐÐÅÌÎÊʱ £¬ÎÒÃÇ¿ÉÒÔʹÓûù±¾µÄÅÌÎÊÒªÁì £¬ÀýÈçwhere¡¢orWhere¡¢whereIn¡¢whereNotInµÈ¡£ÕâЩÅÌÎÊÒªÁì¿ÉÒÔͨ¹ýÁ´Ê½Å²ÓÃÀ´×éºÏ¶à¸öÅÌÎÊÌõ¼þ¡£ÀýÈç £¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´ÅÌÎÊÎÄÕ±íÖÐIDΪ1ÇÒ״̬Ϊ1µÄÎÄÕ£º

$article = DB::table('articles')
    ->where('id', 1)
    ->where('status', 1)
    ->first();

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÊöʾÀýÖÐ £¬ÎÒÃÇʹÓÃwhereÒªÁìÀ´Ö¸¶¨Á½¸öÌõ¼þ¡£firstÒªÁìÓÃÓÚ»ñÈ¡µ¥Ìõ¼Í¼ £¬ÈôÊÇÅÌÎÊЧ¹ûΪ¿Õ £¬Ôò·µ»Ønull¡£

¸ß¼¶ÅÌÎÊ

LaravelÖеÄEloquentÄ£×Ó»¹ÌṩÁËÐí¶à¸ß¼¶ÅÌÎÊÒªÁìÀ´´¦Àí¸üÖØ´óµÄÅÌÎÊ¡£ÒÔÏÂÊÇһЩ³£Óõĸ߼¶ÅÌÎÊÒªÁ죺

2.1 whereBetweenÒªÁì

whereBetweenÒªÁìÔÊÐíÎÒÃÇÅÌÎÊÔÚÖ¸¶¨¹æÄ£Äڵļͼ¡£ÀýÈç £¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´ÅÌÎʽ¨Éèʱ¼äÔÚ2019Äêµ½2020ÄêÖ®¼äµÄÎÄÕ£º

$articles = DB::table('articles')
    ->whereBetween('created_at', ['2019-01-01', '2020-12-31'])
    ->get();

µÇ¼ºó¸´ÖÆ

2.2 orWhereÒªÁì

orWhereÒªÁìÔÊÐíÎÒÃÇÅÌÎʶà¸öÌõ¼þÖеÄí§ÒâÒ»¸öÌõ¼þÖª×ã¼´¿É¡£ÀýÈç £¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´ÅÌÎÊ״̬Ϊ1»ò2µÄÎÄÕ£º

$articles = DB::table('articles')
    ->where('category_id', 1)
    ->orWhere('status', 1)
    ->get();

µÇ¼ºó¸´ÖÆ

2.3 whereHasÒªÁì

whereHasÒªÁìÔÊÐíÎÒÃÇÅÌÎʹØÁªÄ£×ÓÖÐÖª×ãɸѡÌõ¼þµÄ¼Í¼¡£ÀýÈç £¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´ÅÌÎÊËùÓÐÓÐ̸ÂÛµÄÎÄÕ£º

$articles = DB::table('articles')
    ->whereHas('comments')
    ->get();

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÊöʾÀýÖÐ £¬ÎÒÃÇʹÓÃÁËwhereHasÒªÁìÀ´ÅÌÎʹØÁªÄ£×ÓcommentsÖÐÊÇ·ñÓмͼ £¬²¢·µ»ØÖª×ãÌõ¼þµÄÎÄռͼ¡£ÈôÊÇÎÒÃÇÏ£Íû½øÒ»²½É¸Ñ¡Öª×ãÒ»¶¨Ìõ¼þµÄ̸ÂÛ £¬ÎÒÃÇ¿ÉÒÔÔÚwhereHasÒªÁìÖд«ÈëÌØÁíÍâɸѡÌõ¼þ £¬ÀýÈ磺

$articles = DB::table('articles')
    ->whereHas('comments', function($query) {
        $query->where('status', 1);
    })
    ->get();

µÇ¼ºó¸´ÖÆ

ÉÏÊöʾÀýÖÐ £¬ÎÒÃǶÔcommentsÄ£×Ó´«ÈëÅÌÎÊÌõ¼þ £¬½öÅÌÎÊ״̬Ϊ1µÄ̸ÂÛ £¬Í¬Ê±·µ»ØÓëÆä¹ØÁªµÄÎÄռͼ¡£

2.4 whereInºÍwhereNotInÒªÁì

whereInºÍwhereNotInÔÊÐíÎÒÃÇƾָ֤¶¨×ֶεÄÈ¡Öµ¹æģɸѡЧ¹û¡£ÀýÈç £¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´ÅÌÎÊÎÄÕÂ״̬Ϊ1¡¢2¡¢3µÄ¼Í¼£º

$articles = DB::table('articles')
    ->whereIn('status', [1, 2, 3])
    ->get();

µÇ¼ºó¸´ÖÆ

»ìÏýʹÓÃ

ÏÖʵӦÓÃÖÐ £¬ÎÒÃÇÍùÍùÐèÒª×ÛºÏʹÓÃÒÔÉÏÅÌÎÊÒªÁìÀ´Íê³É¶àÌõ¼þÅÌÎÊ¡£ÀýÈç £¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´ÅÌÎÊ·ÖÀàIDΪ1ÇÒ״̬Ϊ1µÄÎÄÕ £¬²¢ÇÒ̸ÂÛÊý´óÓÚ5Ìõ£º

$articles = DB::table('articles')
    ->where('category_id', 1)
    ->where('status', 1)
    ->whereHas('comments', function($query) {
        $query->where('status', 1);
    }, '>', 5)
    ->get();

µÇ¼ºó¸´ÖÆ

ÔÚ±¾ÀýÖÐ £¬ÎÒÃÇʹÓÃÁËwhereHasÒªÁìÀ´ÅÌÎÊÖª×ãÒ»¶¨Ìõ¼þµÄ̸ÂÛ £¬²¢Ê¹ÓÃ>·ûºÅÀ´ÅÌÎÊ̸ÂÛÊýÊÇ·ñ´óÓÚ5Ìõ¡£

½áÂÛ

ÔÚLaravelÖÐ £¬ÎÒÃÇ¿ÉÒÔʹÓöàÖÖ·½·¨À´¾ÙÐжàÌõ¼þÅÌÎÊ¡£Ê¹ÓÃEloquentÄ£×ӵĸ߼¶ÅÌÎÊÒªÁì¿ÉÒÔ¸üÀû±ãµØÍê³ÉÖØ´óµÄÅÌÎʲÙ×÷¡£ÕÆÎÕ¶àÌõ¼þÅÌÎʵļ¼ÇÉ¿ÉÒÔÈÃÎÒÃÇÔÚ¿ª·¢Öиü¸ßЧµØÍê³ÉʹÃü¡£

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

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

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