PHP code example of milwad / laravel-attributes

1. Go to this page and download the library: Download milwad/laravel-attributes library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

milwad / laravel-attributes example snippets




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

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

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

$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);

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

$product->attributes

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

return 'no attribute value';

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

return 'no attribute title';

$product->deleteAllAttribute();

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

$product->deleteAttributeByTitle('title');

$product->deleteAttributeByValue('value');



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,
];
bash
php artisan vendor:publish --provider="Milwad\LaravelAttributes\LaravelAttributesServiceProvider"

php artisan migrate