Installation
Requirements
Before installing Laravel Metadata, make sure you have:
- PHP >= 8.0.1 (8.1+ recommended)
- Laravel >= 9.19 (9/10/11 supported)
- Composer
- jobmetric/laravel-package-core ^1.22
- jobmetric/laravel-event-system ^2.7
Install via Composer
Run the following command to pull in the latest version:
composer require jobmetric/laravel-metadata
Service Provider
Laravel Package Auto-Discovery will automatically register the service provider. If you're using Laravel < 5.5 or have disabled auto-discovery, manually register the provider in config/app.php:
'providers' => [
// ...
JobMetric\Metadata\MetadataServiceProvider::class,
],
Publish Configuration
After installation, publish the configuration file (optional):
php artisan vendor:publish --provider="JobMetric\Metadata\MetadataServiceProvider" --tag="metadata-config"
This will create a config/metadata.php file where you can customize package settings.
Run Migrations
Important: Before using the package, you must run the migrations to create the metas table:
php artisan migrate
This will create the metas table with the following structure:
id- Primary keymetaable_type- Model class name (polymorphic)metaable_id- Model ID (polymorphic)key- Metadata key (indexed)value- Metadata value (text, nullable)is_json- Boolean flag indicating if value is JSONcreated_at- Timestampupdated_at- Timestamp- Unique index on
(metaable_type, metaable_id, key)
Verify Installation
To verify the package is installed correctly, you can check if the trait is available:
use JobMetric\Metadata\HasMeta;
class TestModel extends Model
{
use HasMeta;
}
// This should work without errors
$model = new TestModel();
Quick Test
Create a simple test to ensure everything is working:
use JobMetric\Metadata\HasMeta;
class Product extends Model
{
use HasMeta;
}
// Create a product
$product = Product::create(['name' => 'Test Product']);
// Store metadata
$product->storeMetadata('color', 'red');
// Retrieve metadata
$color = $product->getMetadata('color');
// => 'red'
// If no errors occurred, installation is successful!
Configuration
The package configuration file (config/metadata.php) allows you to customize:
- Table Name: Change the default
metastable name - Other Settings: Additional package-specific configurations
Example configuration:
return [
'tables' => [
'meta' => 'metas',
],
];
Next Steps
Now that you've installed Laravel Metadata, you can:
- Learn about using the HasMeta trait
- Explore real-world examples
- Check out the complete API reference
Troubleshooting
Migration Errors
If you encounter migration errors:
- Make sure you've run
composer require jobmetric/laravel-metadata - Check that all dependencies are installed
- Run
php artisan migrate:freshif needed (warning: this will drop all tables)
Trait Not Found
If you get a "Trait not found" error:
- Make sure the package is properly installed via Composer
- Run
composer dump-autoload - Clear any opcode cache (OPcache) if enabled
Event System Not Working
If events are not firing:
- Ensure
jobmetric/laravel-event-systemis installed - Check that event listeners are registered
- Verify the event system is properly configured