PHP code example of tobymaxham / laravel-properties
1. Go to this page and download the library: Download tobymaxham/laravel-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/ */
tobymaxham / laravel-properties example snippets
class User extends Model
{
use HasProperties;
protected array $propertyDefaults = ['theme' => 'light', 'newsletter' => false];
protected function propertyCasts(): array
{
return ['newsletter' => 'bool', 'plan' => Plan::class, 'api_token' => 'encrypted'];
}
}
$user->getProperty('theme'); // 'light' (default, nothing stored)
$user->setProperty('plan', 'pro')->save(); // stored as "pro"
$user->getProperty('plan'); // Plan::Pro
User::whereProperty('plan', Plan::Pro)->get();
User::whereProperty('newsletter', false)->get(); // also finds users without the property
use Illuminate\Database\Eloquent\Model;
use TobyMaxham\LaravelProperties\HasProperties;
class User extends Model
{
use HasProperties;
}
$user->setProperty('ui.theme', 'dark');
$user->setProperties(['ui.size' => 'lg', 'newsletter' => true]); // keeps the other properties
$user->save();
$user->getProperty('ui.theme'); // 'dark'
$user->getProperty('ui'); // ['theme' => 'dark', 'size' => 'lg']
$user->getProperty('missing', 'fallback'); // 'fallback' (also as closure: fn () => ...)
$user->hasProperty('ui.size'); // true
$user->getProperties(); // all properties, merged with the defaults
$user->getStoredProperties(); // only what is stored, without defaults and casts
$user->onlyProperties(['ui.theme', 'newsletter']);
$user->exceptProperties('ui');
$user->forgetProperty('ui.size'); // or an array of keys
$user->replaceProperties(['theme' => 'dark']);
$user->clearProperties(); // stored as {}
$user->pushProperty('tags', 'laravel', 'php');
$user->pullProperty('legacy'); // get and remove
$user->incrementProperty('logins'); // decrementProperty('credits', 5)
$user->toggleProperty('newsletter');
$props = $user->props();
$props->theme; // like getProperty('theme')
$props->theme = 'dark'; // like setProperty('theme', 'dark')
$props['ui.size']; // array access with "dot" notation
unset($props['legacy']);
isset($props->theme);
$props->set('theme', 'dark')->set(['ui.size' => 'lg'])->increment('logins');
$props->get('ui.size', 'md');
$props->all(); // with defaults and casts
$props->stored(); // without defaults and casts
$props->collect(); // as Collection
count($props);
foreach ($props as $key => $value) { /* ... */ }
json_encode($props);
use TobyMaxham\LaravelProperties\Enums\PropertiesCast;
class User extends Model
{
use HasProperties;
public const string PROPERTIES_COLUMN = 'settings';
public const PropertiesCast PROPERTIES_CAST = PropertiesCast::Object;
}
public const bool SERIALIZE_PROPERTY_DEFAULTS = true; // always with defaults
$user->withPropertyDefaults()->toArray(); // this instance with defaults
$user->withoutPropertyDefaults()->toJson(); // this instance without defaults