PHP code example of wardenyarn / model-properties

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

    

wardenyarn / model-properties example snippets


Schema::create('pages', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->string('slug');
    $table->text('body');
    $table->timestamps();
});

Schema::create('pages', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->string('slug');
    $table->text('body');
    $table->boolean('has_halloween_theme');
    $table->string('meta_title');
    $table->string('meta_description');
    $table->integer('number_of_external_links');
    $table->boolean('export_to_rss');
    ...
    $table->timestamps();
});

Schema::create('property_values', function (Blueprint $table) {
    $table->id();
    $table->morphs('entity');
    $table->morphs('property');
    $table->text('value')->nullable();
    $table->timestamps();
});



namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Wardenyarn\Properties\Models\HasProperties;

class Page extends Model
{
    use HasProperties;

    protected $properties = [
        'meta_title' => [
            'cast' => 'string',
            'default' => 'This is a page',
        ],
        'tags' => [
            'cast' => 'array',
        ],
    ];
}

$page = Page::first();

echo $page->properties->meta_title;

foreach ($page->properties->tags as $tag) {
	echo $tag;
}

$page = Page::first();

$page->properties->meta_title = 'New meta title';
$page->properties->last_admin_check = date();
// OR
$page->properties->set([
	'meta_title' => 'New meta title',
	'last_admin_check' => date(),
]);

$page->properties->save();
// OR
$page->save();

$page = new Page;
$page->name = 'New page';
$page->properties->meta_title = 'a new page';
$page->save();

protected $properties = [
    'last_admin_check' => [
        'cast' => 'datetime',
    ],
];

...

get_class($post->properties->last_admin_check); // Illuminate\Support\Carbon

$page->properties->last_admin_check = date();
$page->properties->save();

gettype($page->properties->last_admin_check); // string

$page->fresh();

gettype($page->properties->last_admin_check); // object Illuminate\Support\Carbon

protected $properties = [
    'meta_title' => [
        'cast' => 'string',
        'default' => 'Page title',
    ],
];

...

$post->properties->meta_title = null;
$post->save();

echo $post->properties->meta_title; // Page title

$page->properties->save(); // Clears the cache
$page->save(); // Clears the cache
bash
php artisan vendor:publish --provider="Wardenyarn\Properties\PropertiesServiceProvider" --tag="migrations"

php artisan migrate
bash
php artisan properties:fill
bash
php artisan cache:clear
bash
php artisan vendor:publish --provider="Wardenyarn\Properties\PropertiesServiceProvider" --tag="config"