composer require akbarali/data-object
Done ? | Name | Version |
---|---|---|
✅ | Add createProperty |
2.5 |
✅ | Add command to create DataObject via Eloquent Model or Database Tables |
2.6.5 |
⬜ | Add command to create DataObject via PHP Array |
In the future |
⬜ | Add creation without converting the Eloquent Model to an array. |
In the future |
Array create Data Object
$object = DataObj::createFromArray([
'key1' => 'value1',
'key2' => 'value2',
]);
Laravel model create Data Object
$model = User::query()->find(1);
$object = DataObj::createFromEloquentModel($model);
Json create Data Object
$object = DataObj::createFromJson('{"key1":"value1","key2":"value2"}');
Create StoreData
class StoreData extends \Akbarali\DataObject\DataObjectBase
{
public ?string $id;
public ?int $user_id;
public ?string $name;
public ?string $phone;
public ?string $address;
public ?string $description = "Default Description";
public ?string $image;
public ?bool $status;
public ?string $created_at;
public UserData $user;
}
UserData
class UserData extends \Akbarali\DataObject\DataObjectBase
{
public ?int $id;
public ?string $name;
public ?string $email;
public ?string $avatar;
public ?string $phone;
public ?string $birth_date;
public ?bool $status;
public ?string $created_at;
public RoleData $role;
}
RoleData
class RoleData extends \Akbarali\DataObject\DataObjectBase
{
public int $id;
public string $name;
public string $slug;
public string $created_at;
}
$store = Store::query()->with(['user.role'])->find(1);
$storeData = StoreData::createFromEloquentModel($store);
Note: If you want to turn the relation into a DataObject, you should open a realton to the model and get that relation with. Then U is also converted to data object
ProductData
ProductData extends \Akbarali\DataObject\DataObjectBase
{
public ?int $id;
public ?string $name;
public ?string $description;
public ?string $image;
public ?bool $status;
public ?string $created_at;
}
StoreData
StoreData extends \Akbarali\DataObject\DataObjectBase
{
public ?int $id;
public ?string $name;
public ?string $description;
public ?string $image;
public ?bool $status;
public ?string $created_at;
public array|ProductData $products;
}
$store = Store::query()->with(['products'])->find(1);
$storeData = StoreData::createFromEloquentModel($store);
//or
$storeData = StoreData::fromModel($store);
$storeData->toArray();
//or
$storeData->all(true);
$storeData->toSnakeCase();
In a model, created_at
is usually use Illuminate\Support\Carbon;
. You can also pass created_at
to Carbon
.
use Illuminate\Support\Carbon;
ProductData extends \Akbarali\DataObject\DataObjectBase
{
public int $id;
public string $name;
public string $description;
public string $image;
public bool $status;
public Carbon $created_at;
}
Supported Readonly Properties
class ClientData extends \Akbarali\DataObject\DataObjectBase
{
public readonly int $id;
public string $full_name;
}
$object = DataObj::createFromArray([
'id' => 1,
'full_name' => 'Akbarali',
]);
$object->id = 2;
Error: Cannot modify readonly property App\DataObjects\HistoryData::$id
Adding: fromModel
fromJson
fromArray
Add static function arrayToClassProperty
class ClientData extends \Akbarali\DataObject\DataObjectBase
{
public int $id;
public string $full_name;
}
$object = ClientData::arrayToClassProperty([
'id' => 1,
'full_name' => 'Akbarali',
]);
Return string: public readonly int $id;public string $full_name;
createProperty
method added to create a property return string and bug fix arrayToClassProperty
Add command create DataObject
Install
Add DataObjectProvider
to /config/app.php
providers
'providers' => [
...
\Akbarali\DataObject\DataObjectProvider::class,
]
DataObject create console command
php artisan do:create
Models Create DataObject
php artisan do:create models
Tables Create DataObject
php artisan do:create tables
Search Table and create DataObject
php artisan do:create tableSearch
Search Model and create DataObject
php artisan do:create modelSearch
Add --model
and --table
options to create DataObject
php artisan do:create --model="App\Models\QQB\User"
php artisan do:create --table="users"