Laravel Attributes Package

If you ever need to create attributes on a model, the Laravel Attributes package is designed to make this easy.

To give you an example of attributes, pretend you have an e-commerce store and want to associate technical details with a product. With this package, you can do it like this:

First, add the Attributable trait to the model:


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;

Then, create a new product and attach attributes to it:

$product = Product::query()->create([
    'name' => 'iPad',
    'content' => 'text',

$data = [
        'title' => 'display',
        'value' => '10.2‑inch Retina display',
        'title' => 'capacity',
        'value' => '64GB, 256GB',
        'title' => 'height',
        'value' => '9.8 inches',
        'title' => 'width',
        'value' => '6.8 inches',


Once this is saved, you can then pull out all the attributes like this:

$product = Product::query()->with('attributes')->get();


For more details check out the package on GitHub.

The post Laravel Attributes Package appeared first on Laravel News.

Join the Laravel Newsletter to get all the latest Laravel articles like this directly in your inbox.

Read more

© 2024 Extly, CB - All rights reserved.