Back to Homepage

Laravel Attributes Package for Models

2 min read


Laravel Package PHP
Laravel Attributes Package for Models

If you ever need to add extra details to a model (like a product in an online store), the Laravel Attributes package makes it really easy.

For example, imagine you're running an online shop and want to include technical information about your products. With this package, you can do it easily like this:

Installation of Laravel Attributes

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

Add Trait to 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;

Now lets add attach the attributes to a specific model

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

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

Now whenever we want to fetch the attributes records we can do it

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


For complete details check out the package on GitHub.

Follow @LaravelSage on X → Follow @LaravelSage on Facebook →
Aniket Singh

Aniket Singh

View All Articles

Full-stack developer with a knack for Merging creativity with technical expertise for standout solutions.

Related Articles

data_forget Helper for Laravel

data_forget Helper for Laravel

Since Laravel version 10.15, there is a new utility function called data_forget that allows you to remove keys from an array or object using a "dot" notation.

Laravel Tenant Application with Tenancy

Laravel Tenant Application with Tenancy

You can make your Laravel app multi-tenant using the Tenancy for Laravel Tenant package. This tenancy package lets you make any Laravel application multi-tenant without rewriting it.

Top Laravel Packages for Building Powerful Applications

Top Laravel Packages for Building Powerful Applications

Are you ready to take your Laravel skills to the next level and build extraordinary applications? Look no further! In this blog post, we will unveil a treasure trove of top packages that will revolutionize your development process.

MJML PHP: Simplifying Email Template Creation

MJML PHP: Simplifying Email Template Creation

The MJML PHP package by Spatie can help you create email templates more easily. But there is a catch. It can be difficult to craft visually appealing and functional email templates that work across a variety of email clients.

Subscribe for 20+ new Laravel tutorials every week

You can unsubscribe at any time. You'll also get -20% off my courses!

© 2024


Laravel Sage

   |    Privacy Policy