PHP code example of amarenkov / laravel-mutable-content
1. Go to this page and download the library: Download amarenkov/laravel-mutable-content 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/ */
amarenkov / laravel-mutable-content example snippets
#[Table('catalog.bicycles')]
#[ClassLabel('Bicycle')]
#[FieldCode, FieldLabel]
class Bicycle extends ModelWithFields
{
#[CFAType(Type::TYPE_LOV_ITEM), CFALabel('Type'), CFALovCode(BikeType::CODE), CFAIsRequired]
public const FIELD_TYPE = 'type';
#[CFAType(Type::TYPE_WEIGHT), CFALabel('Weight')]
public const FIELD_WEIGHT = 'weight';
}
$bike = new Bicycle();
$bike->mergeWithFields(['code' => 'AERO-7', 'label' => 'Aero 7', 'type' => BikeType::ROAD,
'weight' => Weight::fromKilograms(7.4)]);
$bike->withLogContext('Imported')->user($user->id)->save();
// "gears" was added by an administrator in the admin panel: no migration, no release
$bike->gears = 24;
$bike->weight = Weight::fromKilograms(7.2);
$bike->withLogContext('Weighed without pedals')->user($user->id)->save();
RuleHelper::getValidationRules(Bicycle::class);
// ['type' => [InLov, '
use Illuminate\Database\Eloquent\Attributes\Table;
use Amarenkov\MutableContent\Models\ModelWithFields;
use Amarenkov\MutableContent\Attributes\Class\Label as ClassLabel;
use Amarenkov\MutableContent\Attributes\Field\Common\Code as FieldCode;
use Amarenkov\MutableContent\Attributes\FieldAttr\Common\Type as CFAType;
use Amarenkov\MutableContent\Attributes\FieldAttr\Common\Label as CFALabel;
use Amarenkov\MutableContent\Attributes\FieldAttr\Common\IsRequired as CFAIsRequired;
use Amarenkov\MutableContent\Domain\Field\Lov\Type as FieldType;
#[Table('projects.projects')]
#[ClassLabel('Project')]
#[FieldCode]
class Project extends ModelWithFields
{
#[CFAType(FieldType::TYPE_INT), CFALabel('Priority'), CFAIsRequired]
const FIELD_PRIORITY = 'priority';
}
$project = new Project();
$project->mergeWithFields(['code' => 'PRJ-1', 'priority' => 10]);
$project->withLogContext('import')->user($user->id); // goes to the change log with the next save
$project->save();
$project->priority; // 10, read from JSON like a regular attribute
use Amarenkov\MutableContent\Helpers\RuleHelper;
$request->validate(
RuleHelper::getValidationRules(Project::class) +
['tasks' => 'array|
Bicycle::whereField('weight', '<', Weight::fromKilograms(10))
->whereField('frame_size', '>=', Length::fromCentimeters(54))
->whereField('is_electric', true)
->whereField('type', BikeType::GRAVEL)
->get();
Bicycle::whereField('gears', null)->get(); // the field is not set
use Amarenkov\MutableContent\Database\Log\LogContext;
use Amarenkov\MutableContent\Domain\Log\LogData;
// the next save or delete
$project->withLogContext('Imported from CSV')->user($user->id)->data(['source' => 'import'])->save();
// everything inside, including query builder updates and raw SQL
app(LogContext::class)->run(function () {
DB::table('projects.projects')->where('id', 1)->update([...]);
}, ['user_id' => $user->id, 'comment' => 'Bulk fix']);
// a default for a request, a job or a command, e.g. in a middleware
app(LogContext::class)->set(new LogData()->user($request->user()?->id)->data(['ip' => $request->ip()]));
Schema::table('customers', function (Blueprint $table) {
$table->mutableFields(); // JSON column fields, default {}
$table->fieldExtract('nickname')->type('varchar(50)'); // optional generated column
});
Schema::addLog('customers', columns: ['name', 'status']); // optional list of columns to log
use Illuminate\Database\Eloquent\Model;
use Amarenkov\MutableContent\Contracts\MutableModel;
use Amarenkov\MutableContent\Models\Concerns\HasChangeLog;
use Amarenkov\MutableContent\Models\Concerns\HasMutableFields;
#[ClassLabel('Customer')]
class Customer extends Model implements MutableModel
{
use HasMutableFields; // the fields
use HasChangeLog; // withLogContext() and logEvent()
}