8000 GitHub - milwad-dev/laravel-attributes at v0.9
[go: up one dir, main page]

Skip to content

The laravel-attributes package allows Laravel developers to easily add and manage custom attributes on Eloquent models

License

Notifications You must be signed in to change notification settings

milwad-dev/laravel-attributes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

80 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel attributes


Laravel attributes is a package for create attributes easy.
With laravel attributes you can make attributes for all model (Polymorphic)

Requirements


- PHP >= 7.3
- Laravel >= 7.0

Installation


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

Usage

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

Save attribute

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

$product->attachAttribute('age', '17');

Save attribute multiple

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

$data = [
    [
        'title' => 'milwad',
        'value' => 'developer',
    ],
    [
        'title' => 'milwad2',
       
8F0D
 '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

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

Check attribute value is exists

if ($product->hasAttributeValue('17')) {
    return 'attribute value';
}

return 'no attribute value';

Check attribute value is exists

if ($product->hasAttributeTitle('milwad')) {
    return 'attribute title';
}

return 'no attribute title';

Delete all attributes

$product->deleteAllAttribute();

Delete special attributes

$product->deleteAttribute('title', 'value');

License

  • This package is created and modified by Milwad Khosravi for Laravel >= 9 and is released under the MIT License.
0