lunar
lunar copied to clipboard
Model extending via interfaces
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
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 |
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
can you add a test to ensure model events are more triggered twice when extended
example:
App\Models\Product
extends base modelin current implementation, when a product is created it will create 2
Url
records
Will do, but pretty sure this implementation won't do that.
Get the class from the original model
\Lunar\Models\ProductType::modelClass(); // App\Models\ProductType
do you think ::baseModel
is better?
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.
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());
}
Replaced by #1661