尊龙凯时人生就是博

laravel ÅÌÎÊ×÷ÓÃÓò

laravelÖеÄÅÌÎÊ×÷ÓÃÓòÊÇÒ»ÖÖºÜÊÇÖ÷ÒªµÄ¹¦Ð§£¬Ëü¿ÉÒÔÈÃÄãÔÚÄ£×ÓÖнç˵һЩ³£ÓõÄÅÌÎÊÒªÁ죬ÒÔ±ãÔÚÕû¸öÓ¦ÓÃÖоÙÐи´Óá£ÕâЩÅÌÎÊ×÷ÓÃÓò¿ÉÒÔÊÇÈ«¾ÖµÄ£¨ÊÊÓÃÓÚÕû¸öÄ£×Ó£©£¬Ò²¿ÉÒÔÊǾֲ¿µÄ£¨Ö»ÊÊÓÃÓÚ¸ÃÄ£×ÓµÄij¸öÒªÁìÖУ©¡£

ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÏÈÈÝ Laravel ÖеÄÅÌÎÊ×÷ÓÃÓòµÄ½ç˵¡¢Ê¹ÓÃÒÔ¼°Ò»Ð©Ó¦¸Ã×¢ÖصÄÊÂÏî¡£

ÅÌÎÊ×÷ÓÃÓòµÄ½ç˵

ÅÌÎÊ×÷ÓÃÓòÊÇÒ»¸öÄäÃûº¯Êý£¬ËüÎüÊÕÒ»¸ö $query ²ÎÊý£¬¸Ã²ÎÊýÊÇÒ»¸ö Eloquent ÅÌÎʹ¹½¨Æ÷ʵÀý¡£ÔÚÕâ¸öÄäÃûº¯ÊýÖУ¬Äã¿ÉÒÔ¶Ô¸ÃʵÀý¾ÙÐÐһЩÅÌÎʲÙ×÷£¬ÀýÈçÌí¼Ó where ×Ӿ䡢order by ×Ó¾äµÈ¡£

ÒÔÏÂÊÇÒ»¸ö»ù±¾µÄÅÌÎÊ×÷ÓÃÓò½ç˵ʾÀý£º

public function scopePublished($query)
{
    return $query->where('status', '=', 'published');
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÃûΪ published µÄÅÌÎÊ×÷ÓÃÓò£¬Ëü»áÔÚÅÌÎÊʱ×Ô¶¯Ìí¼ÓÒ»¸ö where ×Ó¾äÀ´¹ýÂËÒÑÐû²¼µÄÄ£×Ó¡£

ʹÓÃÅÌÎÊ×÷ÓÃÓò

ʹÓÃÅÌÎÊ×÷ÓÃÓòºÜÊǼòÆÓ£¬Ö»ÐèҪŲÓÃÄ£×ÓµÄÏìÓ¦ÒªÁì¼´¿É¡£ÀýÈ磬ÈôÊÇÎÒÃÇʹÓÃÉÏÃæʾÀýÖÐµÄ published ÅÌÎÊ×÷ÓÃÓò£¬Ö»ÐèҪŲÓÃÈçÏ´úÂ룺

$posts = Post::published()->get();

µÇ¼ºó¸´ÖÆ

ÉÏÊö´úÂ뽫·µ»ØÒ»¸öÒÑÐû²¼µÄËùÓÐ Post ʵÀýÜöÝÍ¡£

ÈôÊÇÄãÐèҪת´ï²ÎÊýµ½ÅÌÎÊ×÷ÓÃÓòÖУ¬ÔòÖ»ÐèÔÚ¸ÃÒªÁìÖÐÌí¼Ó²ÎÊý¼´¿É¡£ÀýÈ磺

public function scopeCategory($query, $categoryId)
{
    return $query->where('category_id', '=', $categoryId);
}

µÇ¼ºó¸´ÖÆ

¸ÃÅÌÎÊ×÷ÓÃÓò¿ÉÓÃÓÚ¹ýÂËij¸öÌض¨Àà±ðµÄ²©¿ÍÎÄÕ¡£

$posts = Post::category(1)->get();

µÇ¼ºó¸´ÖÆ

ÉÏÊö´úÂ뽫·µ»ØÖÖ±ð ID Ϊ 1 µÄËùÓв©¿ÍÎÄÕ¡£

×¢ÖØ£ºÅÌÎÊ×÷ÓÃÓò¿ÉÒÔÁ´Ê½Å²Óã¬ÒÔÊÇÄã¿ÉÒÔͬʱʹÓöà¸öÅÌÎÊ×÷ÓÃÓòÒÔ»ñÈ¡¸ü׼ȷµÄЧ¹û¡£

ÅÌÎÊ×÷ÓÃÓòµÄÈ«¾Ö½ç˵

ÈôÊÇÄãÏëÔÚÕû¸öÄ£×ÓÖÐʹÓÃÏàͬµÄÅÌÎÊ×÷ÓÃÓò£¬Ôò¿ÉÒÔ½ç˵ȫ¾ÖÅÌÎÊ×÷ÓÃÓò¡£Îª´Ë£¬Ö»ÐèÔÚÄ£×ÓÖÐŲÓà boot ÒªÁ죬²¢Ê¹Óà static::addGlobalScope ÒªÁì½ç˵һ¸öÈ«¾ÖÅÌÎÊ×÷ÓÃÓò¼´¿É¡£

ÒÔÏÂÊÇÒ»¸ö»ù±¾µÄÈ«¾ÖÅÌÎÊ×÷ÓÃÓòʾÀý£º

protected static function boot()
{
    parent::boot();

    static::addGlobalScope('active', function (Builder $builder) {
        $builder->where('active', '=', 1);
    });
}

µÇ¼ºó¸´ÖÆ

ÉÏÃæµÄʾÀýÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÃûΪ active µÄÈ«¾ÖÅÌÎÊ×÷ÓÃÓò£¬²¢½«ÆäÏÞÖÆΪ active ×ֶμ´ÊÇ 1 µÄ¼Í¼¡£

ÏÖÔÚ£¬µ±ÄãÔÚÈκÎÒªÁìÖÐÅÌÎʸÃÄ£×Óʱ£¬¸ÃÅÌÎÊ×÷ÓÃÓò¶¼»á×Ô¶¯ÉúЧ¡£

ÒƳýÅÌÎÊ×÷ÓÃÓò

ÓÐʱ¼äÎÒÃÇ¿ÉÄÜÐèÒªÒƳýÌض¨Ä£×ÓµÄÅÌÎÊ×÷ÓÃÓò¡£Laravel ÌṩÁË removeGlobalScope ÒªÁìÀ´×ÊÖúÎÒÃÇʵÏÖÕâÒ»µã¡£

ÀýÈ磬¼ÙÉèÎÒÃÇÓÐÒ»¸öÃûΪ active µÄÈ«¾ÖÅÌÎÊ×÷ÓÃÓò£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÒªÁìÀ´ÒƳýËü£º

$users = User::withoutGlobalScope('active')->get();

µÇ¼ºó¸´ÖÆ

ÉÏÊö´úÂ뽫·µ»Ø²»ÊÜ active ×÷ÓÃÓòÏÞÖƵÄËùÓÐÓû§¡£

×¢ÖØ£ºÈôÊÇÄúÏëÒƳýËùÓÐÈ«¾ÖÅÌÎÊ×÷ÓÃÓò£¬Ôò¿ÉÒÔʹÓà withoutGlobalScopes ÒªÁì¡£

$users = User::withoutGlobalScopes()->get();

µÇ¼ºó¸´ÖÆ

ÉÏÊö´úÂ뽫·µ»Ø²»ÊÜÈκÎÈ«¾ÖÅÌÎÊ×÷ÓÃÓòÏÞÖƵÄËùÓÐÓû§¡£

×ܽá

ÅÌÎÊ×÷ÓÃÓòÊÇ Laravel ÖÐÒ»¸ö¼«ÎªÇ¿Ê¢µÄ¹¦Ð§¡£Í¨¹ýʹÓÃÅÌÎÊ×÷ÓÃÓò£¬Äã¿ÉÒÔÀû±ãµØÔÚÕû¸öÓ¦ÓÃÖÐÖØÓó£ÓõÄÅÌÎʲÙ×÷£¬²¢Ô½·¢ÇåÎúµØ×éÖ¯´úÂ롣ѧϰºÃÅÌÎÊ×÷ÓÃÓòµÄ׼ȷʹÓÃÒªÁì²»µ«¿ÉÒÔÌá¸ßÄãµÄÉú²úÁ¦£¬Ò²¿ÉÒÔ×ÊÖúÄã¹¹½¨Ô½·¢½áʵµÄÓ¦Óá£

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

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

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