Download the PHP package sowailem/flagable without Composer
On this page you can find all versions of the php package sowailem/flagable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sowailem/flagable
More information about sowailem/flagable
Files in sowailem/flagable
Package flagable
Short Description A Laravel package to add flagging functionality to models
License MIT
Informations about the package flagable
Flagable
A flexible and robust Laravel package that provides comprehensive flagging functionality for Eloquent models. This package allows any model to flag any other model with different flag types such as like, follow, favorite, bookmark, upvote, downvote, and custom types through a sophisticated multi-table architecture.
Table of Contents
- Features
- Requirements
- Installation
- Quick Start
- Database Architecture
- Usage Guide
- API Reference
- Advanced Usage
- Default Flag Types
- Performance Considerations
- Troubleshooting
- Contributing
- Security
- Credits
- License
Features
- Flexible Flagging System: Any model can flag any other model with polymorphic relationships
- Multiple Flag Types: Support for like, follow, favorite, bookmark, upvote, downvote, and unlimited custom types
- Sophisticated Architecture: Four-table design for optimal performance and flexibility
- Easy Integration: Simple traits to add flagging capabilities to your models
- Facade Support: Clean API through Laravel facades
- Database Migrations: Automatic database structure setup with proper indexes and constraints
- Default Seeders: Pre-configured flag types ready to use
- Laravel Auto-Discovery: Automatic service provider and facade registration
- Polymorphic Relationships: Full support for different model types as flaggers and targets
- Unique Constraints: Prevents duplicate flags with database-level constraints
- Performance Optimized: Proper indexing and efficient queries
Requirements
- PHP 8.0 or higher
- Laravel 9.0, 10.0, 11.0, or 12.0
Installation
Step 1: Install via Composer
Step 2: Run Migrations
The package will automatically register its service provider and facade through Laravel's auto-discovery feature.
Run the migrations to create the necessary database tables:
Step 3: Seed Default Flag Types (Optional)
To populate the database with default flag types:
This will create the following flag types: like, follow, favorite, bookmark, upvote, downvote.
Quick Start
1. Setup Your Models
Add the appropriate traits to your models:
2. Basic Usage
Database Architecture
The package uses a sophisticated four-table architecture for maximum flexibility and performance:
Tables Structure
1. flag_types Table
Stores available flag types (like, follow, etc.)
2. flag_targets Table
Stores model class names that can be flagged
3. flag_links Table
Links flag types to target model types (pivot table)
4. flags Table
Stores individual flag records
Relationships Diagram
Usage Guide
Using Traits
CanFlag Trait
Add this trait to models that can flag other models:
Available Methods:
flag(Model $target, string $type): Flagunflag(Model $target, string $type): boolhasFlagged(Model $target, ?string $type = null): boolflags(): MorphMany- Get all flags created by this model
Flagable Trait
Add this trait to models that can be flagged by other models:
Available Methods:
isFlaggedBy(Model $flagger, ?string $type = null): boolflagCount(?string $type = null): intflaggers(string $type, string $flaggerModel): Collectionflags(): HasManyThrough- Get all flags for this model
Using the Facade
API Reference
CanFlag Trait Methods
flag(Model $target, string $type): Flag
Creates a flag record for the target model.
Parameters:
$target- The model to be flagged$type- The flag type (e.g., 'like', 'follow')
Returns: Flag model instance
Example:
unflag(Model $target, string $type): bool
Removes a flag record for the target model.
Parameters:
$target- The model to unflag$type- The flag type to remove
Returns: bool - True if flag was removed, false otherwise
Example:
hasFlagged(Model $target, ?string $type = null): bool
Checks if this model has flagged the target model.
Parameters:
$target- The model to check$type- Optional flag type filter
Returns: bool
Example:
flags(): MorphMany
Gets all flags created by this model.
Returns: MorphMany relationship
Example:
Flagable Trait Methods
isFlaggedBy(Model $flagger, ?string $type = null): bool
Checks if this model is flagged by the specified flagger.
Parameters:
$flagger- The model that might have flagged this model$type- Optional flag type filter
Returns: bool
Example:
flagCount(?string $type = null): int
Gets the count of flags for this model.
Parameters:
$type- Optional flag type filter
Returns: int
Example:
flaggers(string $type, string $flaggerModel): Collection
Gets all models that have flagged this model with the specified type.
Parameters:
$type- The flag type$flaggerModel- The class name of the flagger model
Returns: Collection
Example:
flags(): HasManyThrough
Gets all flag records for this model.
Returns: HasManyThrough relationship
Example:
Facade Methods
Flag::addFlagType(string $name): FlagType
Creates a new flag type.
Parameters:
$name- The flag type name
Returns: FlagType model instance
Flag::removeFlagType(string $name): bool
Removes a flag type.
Parameters:
$name- The flag type name to remove
Returns: bool
Flag::flag(Model $flagger, Model $target, string $type): Flag
Creates a flag record.
Parameters:
$flagger- The model creating the flag$target- The model being flagged$type- The flag type
Returns: Flag model instance
Flag::unflag(Model $flagger, Model $target, string $type): bool
Removes a flag record.
Parameters:
$flagger- The model removing the flag$target- The model being unflagged$type- The flag type
Returns: bool
Flag::isFlaggedBy(Model $target, Model $flagger, ?string $type = null): bool
Checks if target is flagged by flagger.
Flag::getFlagCount(Model $target, ?string $type = null): int
Gets flag count for target.
Flag::getFlaggers(Model $target, string $type, string $flaggerModel): Collection
Gets all flaggers for target.
Advanced Usage
Custom Flag Types
You can create custom flag types dynamically:
Bulk Operations
Complex Queries
Model Relationships
Default Flag Types
The package comes with six predefined flag types that are created when you run the seeder:
like- General approval or appreciationfollow- Subscribe to updates or contentfavorite- Mark as preferred or specialbookmark- Save for later referenceupvote- Positive voting (Reddit-style)downvote- Negative voting (Reddit-style)
Using Default Types
Performance Considerations
Database Indexes
The package automatically creates the following indexes for optimal performance:
- Unique constraint on
flag_types.name - Unique constraint on
flag_links(flag_type_id, flag_target_id) - Unique constraint on
flags(flag_link_id, flagger_type, flagger_id) - Index on
flags(flagger_type, flagger_id)
Query Optimization
Caching Strategies
Troubleshooting
Common Issues
1. "Class not found" errors
Make sure you've run composer dump-autoload after installation.
2. Migration errors
Ensure you're running the migrations in the correct order. The package migrations are numbered to run in sequence.
3. Duplicate flag errors
The package prevents duplicate flags at the database level. If you're getting constraint violations, check if the flag already exists before creating it:
4. Performance issues with large datasets
Use eager loading and database-level aggregations:
5. Memory issues with large collections
Use chunking for bulk operations:
Debug Mode
Enable query logging to debug performance issues:
Validation
Always validate flag types before using them:
Contributing
Contributions are welcome! Please follow these guidelines:
- Fork the repository and create your feature branch
- Write tests for any new functionality
- Follow PSR-12 coding standards
- Update documentation for any API changes
- Submit a pull request with a clear description
Development Setup
Reporting Issues
When reporting issues, please include:
- Laravel version
- PHP version
- Package version
- Steps to reproduce
- Expected vs actual behavior
- Any error messages
Security
If you discover any security-related issues, please email [email protected] instead of using the issue tracker.
Security Considerations
- Always validate user input before creating flags
- Consider rate limiting flag creation to prevent abuse
- Implement proper authorization checks in your controllers
- Be cautious with mass assignment when using flag data
Credits
- Abdullah Sowailem - Creator and maintainer
- All contributors who have helped improve this package
License
The MIT License (MIT). Please see License File for more information.
Flagable provides a flexible and robust way to implement flagging functionality in Laravel applications. The sophisticated four-table architecture allows for multiple flag types (like, follow, favorite, etc.) and supports any model flagging any other model with optimal performance and data integrity.