Download the PHP package fiachehr/laravel-eav without Composer
On this page you can find all versions of the php package fiachehr/laravel-eav. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fiachehr/laravel-eav
More information about fiachehr/laravel-eav
Files in fiachehr/laravel-eav
Package laravel-eav
Short Description A complete Entity-Attribute-Value (EAV) package for Laravel
License MIT
Informations about the package laravel-eav
Laravel EAV Package
A complete and powerful Entity-Attribute-Value (EAV) package for Laravel.
Documentation
Laravel EAV — Documentation (hosted on fiachehr.ir)
Features
- ✅ Clean architecture (Domain, Application, Infrastructure, Presentation)
- ✅ MorphTo relationships support for any model
- ✅ Attribute groups management
- ✅ 20+ attribute types (Text, Textarea, Number, Decimal, Date, DateTime, Boolean, File, and more)
- ✅ Flexible validation system with validation rules (Email, URL, Image, Video, etc. can be applied via validations)
- ✅ Optimized database structure with separate columns for different data types
- ✅ Advanced search and filtering capabilities
- ✅ Query Builder for complex EAV queries
- ✅ Indexed columns for fast searches
- ✅ Multilingual support with language-based filtering and optional translation system integration
- ✅ Validation support
- ✅ Easy to use trait for models
- ✅ Backward compatible with existing data
Requirements
- PHP >= 8.3
- Laravel 10.x, 11.x, or 12.x (see
composer.jsonilluminate/*constraints)
Installation
After installation, publish and run the migrations:
Usage
1. Use the Trait in Your Models
2. Working with Attributes
2.1. Getting Attribute Values
2.2. Setting Attribute Values
3. Validation System
You can apply validations to attributes to ensure data integrity. Validations are applied based on the attribute type.
3.1. Setting Validations
3.2. Validating Attribute Values
3.3. Getting Laravel Validation Rules
4. Multilingual Support
The package provides built-in multilingual support for attributes and attribute groups. Each attribute and attribute group has a language field that stores the language code (e.g., 'en', 'fa', 'de').
4.1. Creating Attributes with Language
4.2. Filtering by Language
Using Model Scopes
Using Repository Methods
4.3. Translation System
The package includes a built-in translation system using the eav_translations table. This allows you to store translations for attribute fields (like title and description) in multiple languages.
Using the HasTranslations Trait
To enable translations for your Attribute or AttributeGroup models, use the HasTranslations trait:
Getting Translations
Setting Translations
Deleting Translations
Using with Custom Models
If you extend EloquentAttribute in your project, the translation system automatically uses the base class name for compatibility:
Database Structure
Translations are stored in the eav_translations table:
Note: The translation system uses polymorphic relations, so it works seamlessly with models that extend EloquentAttribute or use the trait directly.
4.4. Working with Multilingual Attributes in Models
4.5. Language Configuration
Make sure your Laravel application has the locale configured:
The package will automatically use app()->getLocale() for the forCurrentLanguage() scope.
5. Advanced Search and Filtering
5.1. Using Model Scopes
5.2. Using Query Builder (Recommended for Complex Queries)
5.3. Text Search Methods
5.4. Number Search Methods
5.5. Decimal Search Methods
5.6. Date/Time Search Methods
5.7. Boolean Search Methods
5.8. JSON Search Methods
5.9. Multiple Conditions
5.10. Ordering and Sorting
5.11. Pagination and Limits
5.12. Getting Results
5.13. Selecting Specific Attributes
5.14. Aggregate Functions
6. Working with Attribute Groups
7. Available Attribute Types
The package supports 20+ attribute types with a flexible validation system:
Text Types
TEXT- Simple text input (can be validated as Email, URL, Slug, etc.)TEXTAREA- Multi-line text (can be validated as Rich Text, Markdown, JSON, etc.)PASSWORD- Password field (with built-in password validation)
Number Types
NUMBER- IntegerDECIMAL- Decimal/Float
Selection Types
RADIO- Radio buttonsSELECT- Dropdown selectMULTIPLE- Multiple selectCHECKBOX- CheckboxCOLOR- Color picker
Date/Time Types
DATE- Date onlyTIME- Time onlyDATETIME- Date and time
Boolean Types
BOOLEAN- True/False
File Types
FILE- File upload (can be validated as Image, Video, Audio, Document, etc.)
Location Types
LOCATION- Location nameCOORDINATES- GPS coordinates
7.1. Validation System
Instead of having separate types for Email, URL, Image, Video, etc., you can use the validation system to apply specific validations to base types:
Example:
- Use
TEXTtype withemailvalidation for email fields - Use
TEXTtype withurlvalidation for URL fields - Use
FILEtype withimagevalidation for image uploads - Use
FILEtype withvideovalidation for video uploads - Use
TEXTAREAtype withjsonvalidation for JSON data
Available Validations:
- Text:
required,min_length,max_length,email,url,slug,password,regex - Number:
required,min,max,integer,decimal - File:
required,image,video,audio,document,max_file_size,allowed_mime_types - Format:
json,array,rich_text,markdown - Date:
required,date_format,after,before
8. Database Optimization
The package uses an optimized database structure with separate columns for different data types:
value_text- For text-based attributes (indexed)value_number- For integer values (indexed)value_decimal- For decimal values (indexed)value_date- For date values (indexed)value_datetime- For datetime values (indexed)value_time- For time valuesvalue_boolean- For boolean values (indexed)value_json- For complex data (JSON)
This structure allows for:
- Fast searches on indexed columns
- Type-specific queries (number ranges, date ranges, etc.)
- Better performance compared to storing everything as text
9. Real-World Examples
Example 1: E-commerce Product Search
Example 2: User Profile Filtering
Example 3: Content Management
10. Using Repositories
11. Using Use Cases
Testing
The package includes a comprehensive test suite with 126 tests covering all features.
Running Tests
Test Coverage
- ✅ Unit Tests: Repositories, Services, Use Cases
- ✅ Feature Tests: Traits, Query Builder, Multilingual Search
- ✅ 126 tests with 322 assertions
- ✅ All CRUD operations
- ✅ Validation system
- ✅ Multilingual support
- ✅ Search and filtering
- ✅ All attribute types
For detailed test documentation, see tests/README.md.
Configuration
Publish the config file:
This will create a config/laravel-eav.php file where you can customize table names and cache settings.
Migrations
Run the migrations to create the necessary tables:
The package includes the following migrations:
create_attributes_table- Stores attribute definitionscreate_attribute_groups_table- Stores attribute groupscreate_attribute_group_attributes_table- Pivot table for groups and attributescreate_attributable_attributes_table- Stores attribute values (optimized structure)create_attributable_attribute_groups_table- Pivot table for entities and groupsupdate_attributable_attributes_table- Updates existing tables with new columns (if needed)create_translations_table- Stores translations for attributes and attribute groups (createseav_translationstable)
Performance Tips
-
Use Indexes: The package automatically creates indexes on value columns for fast searches.
-
Eager Loading: Always eager load attributes when querying multiple entities:
-
Select Specific Attributes: Only load the attributes you need:
- Use Query Builder for Complex Searches: For complex queries with multiple conditions, use the Query Builder instead of multiple scopes:
Troubleshooting
Attribute values not saving correctly
Make sure you're using the correct data type for the attribute. The package automatically stores values in the appropriate column based on the attribute type.
Slow queries
- Check if indexes are created properly
- Use eager loading to avoid N+1 queries
- Consider caching frequently accessed attributes
- Use Query Builder for complex searches instead of multiple scopes
Migration errors
If you're updating from an older version, make sure to run the update migration:
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Changelog
Version 1.0.0
- Initial release
- 20+ attribute types support
- Optimized database structure with separate columns for different data types
- Advanced search and filtering capabilities
- Query Builder for complex EAV queries
- Multilingual support with translation system
- Comprehensive validation system
- Clean architecture
- Full test coverage (126 tests, 322 assertions)
Support
For issues and questions, please open an issue on GitHub.
License
MIT
Made with ❤️ by Fiachehr Pourmojib
All versions of laravel-eav with dependencies
illuminate/support Version ^10.0|^11.0|^12.0
illuminate/database Version ^10.0|^11.0|^12.0