lunar
lunar copied to clipboard
Table prefix duplicated when extending a Lunar model multiple times via HasModelExtending trait
- Lunar version: 1.1.0
- Laravel Version: 12
- PHP Version: 8.4
- Database Driver & Version: sqlite
Bug: Table prefix duplicated when extending a model multiple times
Lunar’s table prefixing works for single inheritance, but breaks when a model extends another extended model.
Working example
App\Models\Product extends Lunar\Models\Collection;
// getTable() => shop_products
Failing example
App\Models\Custom\Product extends App\Models\Product;
// getTable() => shop_shop_products // prefix applied twice
Cause
HasModelExtending::getTable() only checks one parent class:
public function getTable()
{
$parentClass = get_parent_class($this);
return $parentClass == BaseModel::class
? parent::getTable()
: (new $parentClass)->table;
}
This returns the already-prefixed table name from the parent, causing the prefix to be applied multiple times.
Expected
Prefix should be applied once, based on the base model’s table name, regardless of inheritance depth.
We don't support multiple extending. But if it's important to you, we'd welcome a PR.
I just did
#2353