Tuesday, March 28, 2023
HomePHPLaravel Mannequin Flags Bundle | Laravel Information

Laravel Mannequin Flags Bundle | Laravel Information


Laravel Mannequin Flags is a bundle by Spatie to help you add flags to an Eloquent mannequin:

This bundle provides a HasFlags trait to Eloquent fashions enabling you to question fashions which might be both flagged or not flagged. The trait consists of relations, mannequin scopes, and different strategies for working with flags:

1use IlluminateDatabaseEloquentModel;

2use SpatieModelFlagsModelsConcernsHasFlags;

3 

4class Consumer extends Mannequin

5{

6 use HasFlags;

7}

The bundle additionally helps a configurable “flag” mannequin, if you need/have to override the default mannequin that backs mannequin flags.

Utilizing the above mannequin, an instance within the readme reveals that you could “simply construct idempotent (aka restartable) items of code”:

1Consumer::notFlagged('wasSentPromotionMail')

2 ->every(perform(Consumer $person) {

3 Mail::to($person->e-mail)->ship(new PromotionMail())

4 

5 $person->flag('wasSentPromotionMail');

6 });

7});

The instance code solely runs for customers not flagged, due to this fact, the code will skip them on subsequent calls. With out the above code, you’d have to seek out some strategy to monitor whether or not the code despatched the person an e-mail within the occasion of a failure.

This bundle additionally opens up normal mannequin flagging use-cases, equivalent to issues like rolling out a brand new function to a subset of customers:

1$person->hasFlag('someExperimentalFeature'); // returns bool

2 

3// Flag the person for someExperimentalFeature

4$person->flag('someExperimentalFeature');

5 

6// Now the flag returns true

7$person->hasFlag('someExperimentalFeature');

8 

9// Get all customers with the flag

10Consumer::flagged('someExperimentalFeature')->get();

11 

12// Get all customers with out the flag

13Consumer::notFlagged('someExperimentalFeature')->get();

You’ll be able to study extra about this bundle, get full set up directions, and think about the supply code on GitHub. Additionally, learn A Laravel bundle so as to add flags to Eloquent fashions, which has background particulars on why Spatie created this bundle and their major use case.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments