lunar icon indicating copy to clipboard operation
lunar copied to clipboard

Model extending via interfaces

Open glennjacobs opened this issue 1 year ago • 6 comments

Example usage

use App\Models\ProductType;
use Lunar\Facades\ModelManifest;
use Lunar\Models\Contracts\ProductType as ProductTypeContract;

public function boot(): void
{
    ModelManifest::replace(
        ProductTypeContract::class,
        ProductType::class
    );
}

Explicit route model bindings are set in the core, ready to be used.

Route::get('/test/{productType}', function ($productType) {
    dd($productType);  // App\Models\ProductType
});

Get the class from the original model

\Lunar\Models\ProductType::modelClass();  // App\Models\ProductType

So you can then use it to set Eloquent relationships

public function productType(): BelongsTo
{
    return $this->belongsTo(ProductType::modelClass());
}

Filament Resources can use the model contract

use Lunar\Models\Contracts\ProductType;

class ProductTypeResource extends BaseResource
{
    protected static ?string $model = ProductType::class;

TODO

  • [x] Auto-load models from the models directory
  • [x] Make contracts for all models
  • [ ] Add facility to allow Eloquent static calls to continue to work
  • [ ] Update ModelManifest facade's doc blocks
  • [ ] Update all Eloquent relationships to use ::modelClass()
  • [ ] Update all Filament resource model references to use the Contract instead
  • [ ] Ensure any other code in the core is using models correctly
  • [ ] Tests
  • [ ] Documentation

glennjacobs avatar Dec 21 '23 14:12 glennjacobs

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lunar-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 17, 2024 0:41am

vercel[bot] avatar Dec 21 '23 14:12 vercel[bot]

can you add a test to ensure model events are more triggered twice when extended

example: App\Models\Product extends base model

in current implementation, when a product is created it will create 2 Url records

wychoong avatar Dec 21 '23 15:12 wychoong

can you add a test to ensure model events are more triggered twice when extended

example: App\Models\Product extends base model

in current implementation, when a product is created it will create 2 Url records

Will do, but pretty sure this implementation won't do that.

glennjacobs avatar Dec 21 '23 15:12 glennjacobs

Get the class from the original model

\Lunar\Models\ProductType::modelClass();  // App\Models\ProductType

do you think ::baseModel is better?

wychoong avatar Dec 21 '23 15:12 wychoong

Get the class from the original model

\Lunar\Models\ProductType::modelClass(); // App\Models\ProductType

do you think ::baseModel is better?

Base model wouldn't be correct.

glennjacobs avatar Dec 21 '23 16:12 glennjacobs

Just and idea, instead use Product::modelClass() in database, take advantage to add new option to key store in database and separate relation between name of class and type. During life of project is a pain to handle complex data in database.

// Use morphMap from Laravel
ModelManifest::morphMap([
    'product' => Product::modelClass(),
    'collection' => Collection::modelClass(),
    // ...
]);

public function productType(): BelongsTo
{
    return $this->belongsTo(ProductType::morphClass());
}

lguichard avatar Dec 21 '23 16:12 lguichard

Replaced by #1661

glennjacobs avatar Jun 28 '24 20:06 glennjacobs