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