Skip to content

Commit

Permalink
Merge pull request #2 from Laragear/fix/appends
Browse files Browse the repository at this point in the history
[1.x] Clarifies appends [skip-ci]
  • Loading branch information
DarkGhostHunter authored Mar 15, 2024
2 parents 2b54f8c + 5c2c82a commit 66b0c17
Showing 1 changed file with 55 additions and 3 deletions.
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

0 comments on commit 66b0c17

Please sign in to comment.