PHP code example of abdylreshit / laravel-eav

1. Go to this page and download the library: Download abdylreshit/laravel-eav 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/ */

    

abdylreshit / laravel-eav example snippets




namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Abdylreshit\LaravelEav\Traits\HasEavAttributes;

class Product extends Model
{
    use HasEavAttributes;
}

use Abdylreshit\LaravelEav\Models\Attribute;
use Abdylreshit\LaravelEav\Enums\AttributeType;

// Create a color attribute
Attribute::create([
    'name' => 'Color',
    'code' => 'color',
    'type' => AttributeType::STRING,
]);

// Create a price attribute
Attribute::create([
    'name' => 'Price',
    'code' => 'price',
    'type' => AttributeType::DECIMAL,
]);

$product = Product::find(1);

// Set a single attribute
$product->setAttribute('color', 'red');

// Set multiple attributes
$product->setAttributes([
    'color' => 'blue',
    'price' => 99.99,
    'in_stock' => true,
]);

// Get an attribute
$color = $product->getAttribute('color'); // 'blue'

// Get multiple attributes
$attrs = $product->getAttributes(['color', 'price']);

// Get all attributes
$allAttrs = $product->getAllAttributes();

// Check if attribute exists
if ($product->hasAttribute('color')) {
    // ...
}

// Delete an attribute
$product->deleteAttribute('color');

// Find products with a specific color
$redProducts = Product::whereAttribute('color', 'red')->get();

// Find products by multiple attributes
$products = Product::whereAttributes([
    'color' => 'blue',
    'in_stock' => true,
])->get();

// Combine with other queries
$products = Product::where('category_id', 1)
    ->whereAttribute('price', '>', 50)
    ->get();

use Abdylreshit\LaravelEav\Enums\AttributeType;

Attribute::create([
    'name' => 'Description',
    'code' => 'description',
    'type' => AttributeType::TEXT,
]);

Attribute::create([
    'name' => 'Launch Date',
    'code' => 'launch_date',
    'type' => AttributeType::DATETIME,
]);

$attribute = Attribute::create([
    'name' => ['en' => 'Color', 'fr' => 'Couleur'],
    'code' => 'color',
    'type' => AttributeType::STRING,
]);

// Get the name in a specific language
$nameEn = $attribute->getTranslation('name', 'en'); // 'Color'
$nameFr = $attribute->getTranslation('name', 'fr'); // 'Couleur'

// Eager load all EAV values
$products = Product::with('eavValues.attribute')->get();

foreach ($products as $product) {
    $color = $product->getAttribute('color'); // No additional query
}

use Abdylreshit\LaravelEav\Models\Attribute;

$validated = $request->validate([
    'name' => 't = Product::create($validated);
$product->setAttributes($validated);

// Create attributes for a shoe product
Attribute::create(['name' => 'Size', 'code' => 'size', 'type' => AttributeType::STRING]);
Attribute::create(['name' => 'Color', 'code' => 'color', 'type' => AttributeType::STRING]);
Attribute::create(['name' => 'Material', 'code' => 'material', 'type' => AttributeType::STRING]);

// Set attributes on a product
$shoe = Product::find(1);
$shoe->setAttributes([
    'size' => '42',
    'color' => 'black',
    'material' => 'leather',
]);

// Query products
$blackShoes = Product::whereAttribute('color', 'black')->get();

// Create attributes for a page
Attribute::create(['name' => 'Author', 'code' => 'author', 'type' => AttributeType::STRING]);
Attribute::create(['name' => 'SEO Title', 'code' => 'seo_title', 'type' => AttributeType::STRING]);
Attribute::create(['name' => 'Published Date', 'code' => 'published_date', 'type' => AttributeType::DATE]);

// Use on your Page model
$page = Page::find(1);
$page->setAttributes([
    'author' => 'John Doe',
    'seo_title' => 'Best Laravel Tips',
    'published_date' => now()->format('Y-m-d'),
]);

$attribute = Attribute::where('code', 'color')->first();
if (!$attribute) {
    Attribute::create([...]);
}

$products = Product::with('eavValues.attribute')->get();

$price = Attribute::where('code', 'price')->first();
// $price->type will be AttributeType::DECIMAL
// Always pass numeric values for decimal attributes
bash
php artisan vendor:publish --provider="Abdylreshit\LaravelEav\Providers\LaravelEavServiceProvider" --tag="migrations"
bash
php artisan migrate
bash
php artisan eav:publish
bash
php artisan eav:migrate