PHP code example of mralston / laravel-eav

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

    

mralston / laravel-eav example snippets




namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Mralston\Eav\Traits\HasEntityAttributeValues;

class MyModel extends Model
{
    use HasEntityAttributeValues;

    ...
}

$myModel->myField = 'test';
dump($myModel->myField); // test

$myModel->eav('myField', 'test');
dump($myModel->eav('myField')); // test

$myModel->entityAttributeStore->set('myField', 'test');
$myModel->entityAttributeStore->get('myField'); // test

$myModel->entityAttributeStore->unset('myField');
$myModel->entityAttributeStore->get('myField'); // null

// Load model from database and set an EAV attribute
$myModel = MyModel::find(1);
$myModel->myField = 'test';
dump($myModel->myField); // test

// Load a second copy of the model and attempt to
// retrieve the same EAV attribute.
// It fails because it hasn't been saved yet
$myModel2 = MyModel::find(1);
dump($myModel2->myField); // null

// Save the first model and its EAV attributes
$myModel->save(); 

// Load another copy of the model from the database.
// This one will have the EAV data
$myModel3 = MyModel::find(1);
dump($myModel3->myField); // test

$myModel = MyModel::find(1);
$myModel->myField = 'foo';

$myModel2 = MyModel::find(1);
$myModel->myField = 'bar';

dump($myModel->myField); // foo
dump($myModel2->myField); // bar
bash
php artisan migrate