Skip to main content
Back to packages
228 GitHub stars

milwad-dev/laravel-attributes

View on GitHub

Description

Make attributes easy for Laravel

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;
}

After, you have access to attributes relation and etc... .

Save attribute

If you want to attach attribute to a model, you can use attachAttribute method.
attachAttribute method take a title and value.

$product = Product::query()->create([
'name' => 'milwad',
'content' => 'laravel attributes',
]);
 
$product->attachAttribute('age', '17');

Save attribute multiple

If you have multiple attributes you can use attachAttributes method to save attributes for a model.

$product = Product::query()->create([
'name' => 'milwad',
'content' => 'text',
]);
 
$data = [
[
'title' => 'milwad',
'value' => 'developer',
],
[
'title' => 'milwad2',
'value' => 'developer2',
],
[
'title' => 'milwad3',
'value' => 'developer3',
],
[
'title' => 'milwad4',
'value' => 'developer4',
],
[
'title' => 'milwad5',
'value' => 'developer5',
],
[
'title' => 'milwad6',
'value' => 'developer6',
],
];
 
$product->attachAttributes($data);

Get attributes with query

If you want to retrieve attributes from relation you can use attributes.

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

Recent Courses on Laravel Daily