PHP code example of norse-blue / parentity

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

    

norse-blue / parentity example snippets


    $table->string('entity_type')->nullable();
    $table->unsignedInteger('entity_id')->nullable();
    

    

    namespace App;

    use App\Customers\Company;
    use App\Customers\Person;
    use Illuminate\Database\Eloquent\Model;
    use NorseBlue\Parentity\Traits\IsMtiParentModel;

    class Customer extends Model
    {
        use IsMtiParentModel;

        protected $fillable = [
            'name',
        ];

        /** @optional */
        protected $childTypeAliases = [
            'person' => Person::class,
            'company' => Company::class,
        ];

        /** @optional */
        protected $ownAttributes = [
            'id',
            'name',
            'entity_type',
            'entity_id',
        ];
    }
    

    

    namespace App;

    use App\Customer;
    use Illuminate\Database\Eloquent\Model;
    use NorseBlue\Parentity\Traits\IsMtiChildModel;

    class Person extends Model
    {
        use IsMtiChildModel;
        
        protected $parentModel = Customer::class;

        protected $parentEntity = 'entity';

        protected $fillable = [
            'last_name',
        ];
    }
    

$customer = Customer::create(Person::class, [
    'name' => 'Axel',
    'last_name' => 'Pardemann',
]);

$person = Person::create([
    'name' => 'Axel',
    'last_name' => 'Pardemann',
]);

// using the previously created models

// Explicit property from the parent model
echo $customer->name . " " . $customer->entity->last_name;

// Explicit property from the child model
echo $person->parent->name . " " . $person->last_name;

// using the previously created models

// Implicit property from the parent model
echo $customer->name . " " . $customer->last_name;

// Implicit property from the child model
echo $person->name . " " . $person->last_name;