Laravel attributes is a package for create attributes easy.
With laravel attributes you can make attributes for all model (Polymorphic)
- PHP >= 7.3
- Laravel >= 7.0
composer require milwad/laravel-attributes
After publish config files.
php artisan vendor:publish --provider="Milwad\LaravelAttributes\LaravelAttributesServiceProvider"
After publish, you migrate the migration file.
php artisan migrate
First, you use trait in model.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Milwad\LaravelAttributes\Traits\Attributable;
class Product extends Model
{
use HasFactory, Attributable;
}
$product = Product::query()->create([
'name' => 'milwad',
'content' => 'laravel attributes',
]);
$product->attachAttribute('age', '17');
$product = Product::query()->create([
'name' => 'milwad',
'content' => 'text',
]);
$data = [
[
'title' => 'milwad',
'value' => 'developer',
],
[
'title' => 'milwad2',
8000
'value' => 'developer2',
],
[
'title' => 'milwad3',
'value' => 'developer3',
],
[
'title' => 'milwad4',
'value' => 'developer4',
],
[
'title' => 'milwad5',
'value' => 'developer5',
],
[
'title' => 'milwad6',
'value' => 'developer6',
],
];
$product->attachAttributes($data);
Product::query()->with('attributes')->get();
if ($product->hasAttributeValue('17')) {
return 'attribute value';
}
return 'no attribute value';
if ($product->hasAttributeTitle('milwad')) {
return 'attribute title';
}
return 'no attribute title';
$product->deleteAllAttribute();
$product->deleteAttribute('title', 'value');
- This package is created and modified by Milwad Khosravi for Laravel >= 9 and is released under the MIT License.