Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Clarifies appends [skip-ci] #2

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ namespace Vendor\Package\Models;

use Illuminate\Database\Eloquent\Model;
use Laragear\MetaModel\CustomizableModel;
use Vendor\Package\Migrations\ModelMigration;
use Vendor\Package\Migrations\CarMigration;

class MyModel extends Model
class Car extends Model
{
use CustomizableModel;

protected static function migrationClass(): string
{
return ModelMigration::class;
return CarMigration::class;
}
}
```
Expand Down Expand Up @@ -98,6 +98,58 @@ class AppServiceProvider extends ServiceProvider
}
```

### Appends

As you are guessing, the `useAppend` only works when your model has attributes accessors. If you expect the user to append attributes in your model serialization, ensure you have the proper accessors.

For example, we could add the `color` and `chassis` attribute accessors in our Car model.

```php
namespace Vendor\Package\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Laragear\MetaModel\CustomizableModel;
use Vendor\Package\Migrations\ModelMigration;

class Car extends Model
{
use CustomizableModel;

// ...

protected function getColorAttribute()
{
return $this->metadata->color;
}

protected function chassis(): Attribute
{
return Attribute::get(fn() => (string) $this->metadata->chassis)
}
}
```

Later, the end-developer can append these at runtime.

```php
namespace App\Providers;

use MyVendor\MyPackage\Models\Car;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Car::$useAppends = ['color', 'chassis'];
}
}
```

## Customizable Migration

To allow customizable migrations, create a standard migration file, but, instead of returning a class that extends the default `Migration` class, return a `migration()` call to your model class.
Expand Down
Loading