Coder Social home page Coder Social logo

milwad-dev / laravel-attributes Goto Github PK

View Code? Open in Web Editor NEW
183.0 3.0 9.0 67 KB

Make attributes easy for Laravel

Home Page: https://packagist.org/packages/milwad/laravel-attributes

License: MIT License

PHP 100.00%
attribute composer eloquent laravel laravel-package php polymorphic laravel-library larave-attribute laravel-packages

laravel-attributes's Introduction

Laravel Attributes

Laravel-Attributes

Latest Stable Version Total Downloads License Passed Tests PHP Version Require


Laravel attributes is a package for create attributes easy.
With laravel attributes you can make attributes for all model (Polymorphic).
You don't have any stress for attributes! You can create attributes for any model and display like drink water :)

Requirements


  • PHP: ^8.0
  • Laravel Framework: ^9.0
Attributes L9 L10
1.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;
}

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

Check attribute value is exists

Maybe you want to check one model has an attribute value you can use hasAttributeValue method.

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

return 'no attribute value';

Check attribute value is exists

Maybe you want to check one model has an attribute title you can use hasAttributeTitle method.

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

return 'no attribute title';

Delete all attributes

If you want to delete all attributes of one model you can use deleteAllAttribute method.

$product->deleteAllAttribute();

Delete special attributes

If you want to delete specific attribute of a model you can use deleteAttribute method.

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

Delete special attributes by title

If you want to delete specific attribute by title you can use deleteAttributeByTitle method.

Maybe you have two attributes with same title, if you delete with this method, will be deleted two attributes

$product->deleteAttributeByTitle('title');

Delete special attributes by value

If you want to delete specific attribute by value you can use deleteAttributeByValue method.

Maybe you have two attributes with same value, if you delete with this method, will be deleted two attributes

$product->deleteAttributeByValue('value');

Testing

Run the tests with:

vendor/bin/pest
composer test
composer test-coverage

Customize

If you want change migration table name or change default model you can use laravel-attributes config that exists in config folder.

<?php

return [
    /*
     * Table config
     *
     * Here it's a config of migrations.
     */
    'tables' => [
        /*
         * Get table name of migration.
         */
        'name' => 'attributes',

        /*
         * Use uuid as primary key.
         */
        'uuids' => false, // Also in beta !!!
    ],

    /*
     * Model class name for attributes table.
     */
    'attributes_model' => \Milwad\LaravelAttributes\Attribute::class,
];

License

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

Contributing

This project exists thanks to all the people who contribute. CONTRIBUTING

Security

If you've found a bug regarding security please mail [email protected] instead of using the issue tracker.

laravel-attributes's People

Contributors

codewithdennis avatar dependabot[bot] avatar github-actions[bot] avatar hamog avatar michael-rubel avatar milwad-dev avatar stylecibot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

laravel-attributes's Issues

Ambiguous name

I justed wanted to mention that the word attribute is pretty easy to misunderstand.

  • Eloquent models already have attributes.
  • They can also have PHP Attributes

I think adding another type of attributes, that are actually related models, is bound to cause a lot of confusion.

Update attribute

Could someone explain to me how to modify one or more attributes? thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.